Restarting a QML application
-
Hi all - I'm trying to adapt this approach for restarting a Qt application to one that doesn't use widgets. Here's what I've coded so far:
int main(int argc, char *argv[]) { int currentExitCode = 0; QGuiApplication app(argc, argv); MyClass myClass(nullptr, &app); QQmlApplicationEngine engine; qmlRegisterType<MyClass>("MyClass", 1, 0, "MyClass"); engine.rootContext()->setContextProperty("myClass", &myClass); do { engine.loadFromModule("restartQml", "Main"); currentExitCode = app.exec(); } while (currentExitCode == MyClass::EXIT_CODE_REBOOT); return currentExitCode; } // class MyClass : public QObject { Q_OBJECT QGuiApplication *m_app = nullptr; public: explicit MyClass(QObject *parent = nullptr, QGuiApplication *app = nullptr); static const inline int EXIT_CODE_REBOOT = -123456789; public slots: void restartRequested() { m_app->exit( EXIT_CODE_REBOOT ); }
The problem is that the original instance of the app doesn't exit. Every time I press the restart button in the app, I get a new instance of the app, but the old one doesn't go away.
Can anyone tell me why the QGuiApplication isn't responding to the exit() call in the slot?
Thanks...
-
Hi,
One key difference between your version and the one you linked, you are not recreating the QGuiApplication object nor the engine.
-
Thanks, @SGaist . It turns out that it was sufficient to recreate the engine. I modified the relevant portion of my code as follows:
QQmlApplicationEngine *engine; qmlRegisterType<MyClass>("MyClass", 1, 0, "MyClass"); do { engine = new QQmlApplicationEngine; engine->rootContext()->setContextProperty("myClass", &myClass); engine->loadFromModule("restartQml", "Main"); currentExitCode = app.exec(); delete engine; } while (currentExitCode == MyClass::EXIT_CODE_REBOOT); return currentExitCode; }
And it seems to work fine.
I also discovered that I didn't need to pass in the QGuiApplication to my handler class; evidently this is provided to me via qApp:
#include <qguiapplication.h> void MyClass::restartRequested() { qApp->exit( EXIT_CODE_REBOOT ); }
Thank you for the help.
-
M mzimmers has marked this topic as solved on