You're correct: with QApplication
it works for me in both Qt 4.8 and 5.2, but not with 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();
}
You're incorrect in stating that the code will compile with just the gui
module added to the project. Without the widgets
module, your code won't compile, and this should have been the first hint that you're trying to do something that's not supported.
QGraphicsScene
is not part of the gui
module, but of the widgets
module! All classes in the widgets module are free to assume (and they do!) that you've instantiated QApplication
, not QGuiApplication
! I've submitted it as QTBUG-36413, but - unsurprisingly - it got closed as invalid. It simply is not supposed to work.
If you want to use the QGuiApplication
, there's a workaround:
The QApplication
(but not QGuiApplication
) is keeping a list of all graphics scenes. QGraphicsScene
assumes that the type of qApp
is QApplication
. The list is accessed in QGraphicsScenePrivate::init()
. The fix is to guard the reference to scene_list
with a type check on qApp
:
if (qobject_cast<QApplication*>(qApp))
You need this in both QGraphicsScenePrivate::init()
and QGraphicsScene::~QGraphicsScene()
. I've confirmed that after you recompile Qt, it doesn't crash anymore.
If you're serious about it, you'd have to copy relevant files from Qt's sources to your project, remove the widgets
module from the project file, and patch things up until they work. Personally I think it's a waste of effort. It's trivial to render a QGraphicsView
into an arbitrary paint device, without having to actually display it on the screen.