首页 > 解决方案 > 尝试创建新的 QGraphicsScene 时应用程序崩溃

问题描述

我有以下类,派生自QObjectQQuickImageProvider

#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");
}

该应用程序在尝试使用创建新QGraphicsScenenew QGraphicsScene(this)崩溃。有人知道为什么会这样吗?我正在Qt 5.15.1 64bit使用Ubuntu Linux OS.

标签: qt

解决方案


该程序无法在没有QApplication实例的情况下创建小部件。
更改QGuiApplication您的main.cpptoQApplication将解决它。

获取的详细信息:

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();
}

qApp声明为:

#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))

推荐阅读