Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Restarting a QML application
Forum Updated to NodeBB v4.3 + New Features

Restarting a QML application

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 359 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One key difference between your version and the one you linked, you are not recreating the QGuiApplication object nor the engine.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved