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. How to end a QThread correct in main.cpp?
Qt 6.11 is out! See what's new in the release blog

How to end a QThread correct in main.cpp?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 885 Views 1 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.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    Hello,
    I have a problem with ending a QThread.

    In my main.cpp I connect the "aboutToQuit" signal to call the destructor of the used class. Its destructor emits a "finished" signal which then tells the corresponding thread to quit.
    Now I need to wait until the thread quits before the application quits.
    Here is my problem. I have no clue how to do that.

    How can I properly end my thread and my application?

    Thanks

    Used MyClass slot:

    public slots:
        void quit(){delete this;} //Calls destructor which emits “finished” signal
    

    Main.cpp:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QThread>
    #include "myclass.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MyClass* myClass = new MyClass();
    
        QThread myClassThread;
        QObject::connect(&app, &QApplication::aboutToQuit, myClass, &MyClass::quit);
        QObject::connect(myClass, &MyClass::finished,  &myClassThread, &QThread::quit);
        myClass->moveToThread(&myClassThread);
        myClassThread.start();
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • R robro

      Hello,
      I have a problem with ending a QThread.

      In my main.cpp I connect the "aboutToQuit" signal to call the destructor of the used class. Its destructor emits a "finished" signal which then tells the corresponding thread to quit.
      Now I need to wait until the thread quits before the application quits.
      Here is my problem. I have no clue how to do that.

      How can I properly end my thread and my application?

      Thanks

      Used MyClass slot:

      public slots:
          void quit(){delete this;} //Calls destructor which emits “finished” signal
      

      Main.cpp:

      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include <QThread>
      #include "myclass.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          MyClass* myClass = new MyClass();
      
          QThread myClassThread;
          QObject::connect(&app, &QApplication::aboutToQuit, myClass, &MyClass::quit);
          QObject::connect(myClass, &MyClass::finished,  &myClassThread, &QThread::quit);
          myClass->moveToThread(&myClassThread);
          myClassThread.start();
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @robro Call https://doc.qt.io/qt-5/qthread.html#quit and then https://doc.qt.io/qt-5/qthread.html#wait

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      2
      • jsulmJ jsulm

        @robro Call https://doc.qt.io/qt-5/qthread.html#quit and then https://doc.qt.io/qt-5/qthread.html#wait

        R Offline
        R Offline
        robro
        wrote on last edited by
        #3

        @jsulm said in How to end a QThread correct in main.cpp?:

        @robro Call https://doc.qt.io/qt-5/qthread.html#quit and then https://doc.qt.io/qt-5/qthread.html#wait

        Thanks, but how?
        Calling wait after "return app.exec();" would not work and I have no clue to call wait using the signal slot mechanism.

        JKSHJ 1 Reply Last reply
        0
        • R robro

          @jsulm said in How to end a QThread correct in main.cpp?:

          @robro Call https://doc.qt.io/qt-5/qthread.html#quit and then https://doc.qt.io/qt-5/qthread.html#wait

          Thanks, but how?
          Calling wait after "return app.exec();" would not work and I have no clue to call wait using the signal slot mechanism.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @robro said in How to end a QThread correct in main.cpp?:

          Calling wait after "return app.exec();" would not work

          Return after waiting:

          int exitCode = app.exec();
          myClassThread.wait();
          return exitCode;
          
          void quit(){delete this;} //Calls destructor which emits “finished” signal
          

          Beware: In general, deleting a QObject in a slot is dangerous. It could cause your program to crash if other signals arrive.

          You don't need MyClass::quit() -- just connect QApplication::aboutToQuit() to MyClass::deleteLater().

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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