Qt 尝试创建新QGraphicscene时应用程序崩溃

Qt 尝试创建新QGraphicscene时应用程序崩溃,qt,Qt,我有以下课程,来源于和: 使用new qgraphicscene(this)创建新图形时,应用程序崩溃。有人知道为什么会这样吗?我使用的是Qt 5.15.1 64位和Ubuntu Linux操作系统如果没有QApplication实例,程序无法创建小部件。 将main.cpp中的QGuiApplication更改为QApplication将修复它 详情摘自: 非常感谢,我错过了!:D #ifndef UVICOLORPICKER_H #define UVICOLORPICKER_H #incl

我有以下课程,来源于和:


使用
new qgraphicscene(this)
创建新图形时,应用程序崩溃。有人知道为什么会这样吗?我使用的是
Qt 5.15.1 64位
Ubuntu Linux操作系统
如果没有
QApplication
实例,程序无法创建小部件。
main.cpp
中的
QGuiApplication
更改为
QApplication
将修复它

详情摘自:


非常感谢,我错过了!:D
#ifndef UVICOLORPICKER_H
#define UVICOLORPICKER_H

#include <QObject>
#include <QQuickImageProvider>
#include <QLinearGradient>
#include <QColor>
#include <QBrush>
#include <QGraphicsScene>
#include <QImage>
#include <QPainter>
#include <QSize>

/**
 * @class Color picker class
 */
class UviColorPicker : public QObject,
                       public QQuickImageProvider
{
    Q_OBJECT

private:
    /**
     * @brief Color picker background gradient stops
     */
    QGradientStops colorPickerBackgroundGradientStops;

    /**
     * @brief Color picker background gradient
     */
    QLinearGradient colorPickerBackgroundGradient;

    /**
     * @brief Color picker background brush
     */
    QBrush colorPickerBackgroundBrush;

    /**
     * @brief Color picker foreground gradient stops
     */
    QGradientStops colorPickerForegroundGradientStops;

    /**
     * @brief Color picker foreground gradient
     */
    QLinearGradient colorPickerForegroundGradient;

    /**
     * @brief Color picker foreground brush
     */
    QBrush colorPickerForegroundBrush;

    /**
     * @brief Color picker graphics scene
     */
    QGraphicsScene* colorPickerGraphicsScene;

    /**
     * @brief Color picker image
     */
    QImage* colorPickerImage;

private:
    /**
     * @brief Background gradient stops init method
     */
    void initBackgroundGradientStops();


    /**
     * @brief Foreground gradient stops init method
     */
    void initForegroundGradientStops();

public:
    /**
     * @brief Constructor
     * @param parent
     */
    explicit UviColorPicker(QObject* parent=Q_NULLPTR);

    /**
     * @brief Destructor
     */
    ~UviColorPicker();

signals:

};

#endif // UVICOLORPICKER_H
UviColorPicker::UviColorPicker(QObject* parent)
    : QObject(parent),
      QQuickImageProvider(QQmlImageProviderBase::Image,
                          QQmlImageProviderBase::ForceAsynchronousImageLoading),
      colorPickerGraphicsScene(new QGraphicsScene(this)),   // app crashes here, why?!
      colorPickerImage(new QImage(QSize(256,
                                        256),
                                  QImage::Format_ARGB32))
{
    QPainter gradientPainter(this->colorPickerImage);

    gradientPainter.setRenderHint(QPainter::Antialiasing);

    this->initBackgroundGradientStops();
    this->initForegroundGradientStops();

    this->colorPickerBackgroundGradient.setStops(this->colorPickerBackgroundGradientStops);
    this->colorPickerBackgroundBrush=QBrush(this->colorPickerBackgroundGradient);
    this->colorPickerGraphicsScene->setBackgroundBrush(this->colorPickerBackgroundBrush);

    this->colorPickerForegroundGradient.setStops(this->colorPickerForegroundGradientStops);
    this->colorPickerForegroundBrush=QBrush(this->colorPickerForegroundGradient);
    this->colorPickerGraphicsScene->setForegroundBrush(this->colorPickerForegroundBrush);

    this->colorPickerGraphicsScene->render(&gradientPainter);
    this->colorPickerImage->save("gradient.png");
}
void QGraphicsScenePrivate::init()
{
    Q_Q(QGraphicsScene);
    index = new QGraphicsSceneBspTreeIndex(q);
    // Keep this index so we can check for connected slots later on.
    changedSignalIndex = signalIndex("changed(QList<QRectF>)");
    processDirtyItemsIndex = q->metaObject()->indexOfSlot("_q_processDirtyItems()");
    polishItemsIndex = q->metaObject()->indexOfSlot("_q_polishItems()");
    qApp->d_func()->scene_list.append(q);
    q->update();
}
#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))