Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt quit both c++ and UI
Forum Updated to NodeBB v4.3 + New Features

Qt quit both c++ and UI

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 4.7k Views 2 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.
  • A Offline
    A Offline
    alecs26
    wrote on last edited by
    #1

    Hello,

    I have a Qt5 interface that runs both C++ and a UI made with QML.
    At one point, I have to quit the program. Before, what I was doing was Qt.quit() in the QML and everything was ok.

    However, now I have some threads that I must close in the main.cpp before quitting. Instead of doing Qt.quit() in the QML, I want to close everything from my main.cpp such as

    *main.cpp
    loop1.quit();
    loop2.quit();
    quitqt ?
    

    the "quitqt" is what I miss. I tried different things from information I found on forums but the QML UI never quits and continue to run.
    I tried for instance: app.quit() and QApplication.quit()

    What could I do ?

    Thank you very much for your help,

    Alex

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

      Hi,

      What about :

      QGuiApplication app(argc, argv);
      
      // your stuff
      
      int ret = app.exec();
      
      // Stop your threads cleanly
      
      return ret;
      

      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
      0
      • A Offline
        A Offline
        alecs26
        wrote on last edited by
        #3

        Thank you for your answer. However, the QML UI still remains.
        That seems to be normal with this code. The following is the template code for a new Qt project:

        *main.cpp
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        
            return app.exec();
        }
        

        This main.cpp program will run and execute "return app.exec();" and the QML UI will remain.
        In QML, the function "Qt.quit()" closes everythin but I don't know how to do the same through the main.cpp.

        Thank you for your help !

        Alex

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

          If I understand things correctly you would like to be able to quit your threads before ending the application, right ?

          Hence my suggestion to store the return value from app.exec(), then quit your threads and then return the value you stored before.

          Or do you want your threads to quit your application ?

          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
          0
          • A Offline
            A Offline
            alecs26
            wrote on last edited by
            #5

            Sorry, I think I was not clear.
            I want to quit my threads (this is done ok) and then I want to quit the application.
            The problem is that in the main.cpp, only doing "return app.exec();" does not close my Qt application. The QML UI remains open. I want to close this QML UI. I can do it in the QML code with Qt.Quit() but I would like to do it in my main.cpp instead.

            Thank you very much,

            Alex

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

              Then call qApp->quit() once you're done with your threads.

              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
              0
              • A Offline
                A Offline
                alecs26
                wrote on last edited by
                #7

                Thank you again for your time.
                However it's still the same, the UI remains...

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

                  Can you show the code you are using ?

                  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
                  0
                  • A Offline
                    A Offline
                    alecs26
                    wrote on last edited by
                    #9

                    Here is the main.cpp code:

                    #include <QGuiApplication>
                    #include <QQmlApplicationEngine>
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                        QGuiApplication app(argc, argv);
                    
                        QQmlApplicationEngine engine;
                        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
                    
                        qApp->quit();
                    
                        return app.exec();
                    }
                    

                    The QML code is a simple empty window.

                    When I run this main.cpp code, even with "qApp->quit();", the program does not quit (the QML window remains open).

                    From this forum, there seems to be other solutions. I tried "exit(1)" which seems to work but I am not sure it exits cleanly.
                    http://stackoverflow.com/questions/10038372/how-to-terminate-a-qt-programming-if-initialization-failed

                    Thank you again for your time !

                    Alex

                    kshegunovK 1 Reply Last reply
                    0
                    • A alecs26

                      Here is the main.cpp code:

                      #include <QGuiApplication>
                      #include <QQmlApplicationEngine>
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                          QGuiApplication app(argc, argv);
                      
                          QQmlApplicationEngine engine;
                          engine.load(QUrl(QLatin1String("qrc:/main.qml")));
                      
                          qApp->quit();
                      
                          return app.exec();
                      }
                      

                      The QML code is a simple empty window.

                      When I run this main.cpp code, even with "qApp->quit();", the program does not quit (the QML window remains open).

                      From this forum, there seems to be other solutions. I tried "exit(1)" which seems to work but I am not sure it exits cleanly.
                      http://stackoverflow.com/questions/10038372/how-to-terminate-a-qt-programming-if-initialization-failed

                      Thank you again for your time !

                      Alex

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      @alecs26 said in Qt quit both c++ and UI:

                      When I run this main.cpp code, even with "qApp->quit();", the program does not quit (the QML window remains open).

                      Then there's something fishy. Either some thread is hanging or your QML code is hanging. As a side note, how do you ensure the threads had quit before returning and how do you start them (there's nothing even remotely resembling this in the C++ code you pasted).

                      By the way

                      qApp->quit();
                      

                      does nothing here, there's no event loop running. You could try:

                      QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
                      

                      instead.

                      I tried "exit(1)" which seems to work but I am not sure it exits cleanly.

                      Never do that without a good reason. ::exit's purpose is to terminate the application in extreme circumstances, not whenever one feels like it. For one it will not call any of your object's (app and engine) destructors, which is bad enough by itself and has serious implications.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        alecs26
                        wrote on last edited by
                        #11

                        @kshegunov @SGaist
                        The code I posted is for a simple program that just launches a qml window. It was to simplify the use case. Even with this simple case, I can't get the QML window to close (except with "exit(1)").
                        The QML code is simply:

                        import QtQuick 2.7
                        import QtQuick.Controls 2.0
                        import QtQuick.Layouts 1.0
                        
                        ApplicationWindow
                        {
                            visible: true
                            width: 640
                            height: 480
                            title: qsTr("Hello World")
                        
                        }
                        

                        Your suggestion:

                        QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
                        

                        seems to work. Can you tell me what it does different of qApp->quit ?
                        I guess it quits cleanly ?

                        Thank you !

                        Alex

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

                          Calling qApp->quit() before exec() won't have any effect since the event loop isn't even started.

                          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
                          3

                          • Login

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