qt - 尝试创建新的 QGraphicsScene 时应用程序崩溃
问题描述
我有以下类,派生自QObject和QQuickImageProvider:
#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");
}
该应用程序在尝试使用创建新QGraphicsScene时new QGraphicsScene(this)
崩溃。有人知道为什么会这样吗?我正在Qt 5.15.1 64bit
使用Ubuntu Linux OS
.
解决方案
该程序无法在没有QApplication
实例的情况下创建小部件。
更改QGuiApplication
您的main.cpp
toQApplication
将解决它。
从源获取的详细信息:
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()))
推荐阅读
- java - 登录时用户帐户被锁定
- reactjs - 渲染 ReactJS - './images/0.jpg' 有效.. 但 property.image 没有?
- dictionary - 多个字符串到字典的转换
- java - 如何从 Java 的子目录中读取文件?
- tradier - 交易者历史 API 不适用于“$DJI”等索引符号
- powershell - 无法使用 powershell 脚本从 IIS 服务复制远程目录中的文件
- visual-studio - 手动 OpenGL 扩展加载 LNK2005 和 LNK1169
- webpack - Webpack 4 Hot Module Replacement 仅适用于样式表
- c# - 尝试创建播放列表时 WMP 崩溃
- typescript - 键入已知类型的记录