如何将QGraphicscene与QtGui/QGUI应用程序一起使用?

如何将QGraphicscene与QtGui/QGUI应用程序一起使用?,qt,qgraphicsscene,Qt,Qgraphicsscene,我用QtCreator创建了一个新的qt5项目,并在其中添加了以下行 #包括 //在main()中: qgraphicscene场景 在.pro文件中,我添加了: Qt+=gui qgraphicscene的对象创建导致分段错误。 我错过了什么 编辑: 我现在意识到,程序的其余部分使用的不是QtGui/QGuiApplication,而是QtGui/QGuiApplication 如何在QtGui/qgui应用程序中使用qgraphicscene?你是对的:对于QApplication它在qt

我用QtCreator创建了一个新的qt5项目,并在其中添加了以下行

#包括

//在main()中:
qgraphicscene场景

在.pro文件中,我添加了:
Qt+=gui

qgraphicscene
的对象创建导致分段错误。 我错过了什么

编辑:


我现在意识到,程序的其余部分使用的不是
QtGui/QGuiApplication
,而是
QtGui/QGuiApplication


如何在QtGui/qgui应用程序中使用qgraphicscene?

你是对的:对于
QApplication
它在qt4.8和5.2中都适用,但对于
QGuiApplication

#include <QApplication>
#include <QGraphicsScene>
#include <QTimer>

int main(int argc, char ** argv) {
    QApplication app(argc, argv);
    // With QGuiApplication, QGraphicsScene constructor crashes.
    QGraphicsScene scene;
    QTimer::singleShot(1000, &app, SLOT(quit()));
    return app.exec();
}
qgraphicsceneprivate::init()
qgraphicscene::~qgraphicscene()
中都需要这个。我已经确认,在重新编译Qt之后,它不再崩溃

如果你是认真的,你必须将相关文件从Qt的源代码复制到你的项目中,从项目文件中删除
widgets
模块,并修补这些东西,直到它们正常工作。我个人认为这是浪费精力。将
QGraphicsView
渲染到任意绘制设备中,而不必在屏幕上实际显示,这是很简单的

if (qobject_cast<QApplication*>(qApp))