Unsolved QQmlApplicationEngine in singleton
-
Why when I close application i get Segmentation filed ???
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "test.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); Test::instance().init(); return app.exec(); }
test.h
#include <QtQuick> class Test { QQmlApplicationEngine engine; public: void init(); static Test &instance(); };
test.cpp
#include <QtQuick> #include <QGuiApplication> #include <QQmlApplicationEngine> void Test::init() { engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); } Test &Test::instance() { static Test test; return test; }
-
It doesn't sound like a good idea to have a QML engine instance outlive the QGuiApplication instance. Better destroy the QML engine before returning from main(). You could, for example, allocate the engine on the heap and add something like cleanup() as a pair to init(), or allocate the engine on the stack in main() and pass it as an argument to init().