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 quit QApplication with QThread
Forum Update on Monday, May 27th 2025

How quit QApplication with QThread

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k Views
  • 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.
  • M Offline
    M Offline
    mk33
    wrote on last edited by
    #1

    If is line with singleShot comment, program exit correctly (Worker destructor is called). What is correct way, how quit application if is called QCoreApplication::quit(). I need call Worker destructor.

    #include <QCoreApplication>
    
    #include <QThread>
    #include <QDebug>
    #include <QTimer>
    #include <QAbstractEventDispatcher>
    
    #include "Worker.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QThread* workerThread = new QThread();
        Worker* worker = new Worker();
    
        QObject::connect(workerThread, SIGNAL(started()), worker, SLOT(work()));
        QObject::connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
        QObject::connect(workerThread, SIGNAL(destroyed(QObject*)), worker, SLOT(deleteLater()));
        QObject::connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
        QObject::connect(worker, SIGNAL(finished()), qApp, SLOT(quit()));
        worker->moveToThread(workerThread);
        workerThread->start();
    
        QTimer::singleShot(2000, qApp, [=](){QCoreApplication::quit();});
    
        int ret = a.exec();
    
        while (qApp->eventDispatcher()->processEvents(QEventLoop::AllEvents))
        {
    
        }
    
        qDebug() << "end with" << ret;
        return ret;
    }
    
    #ifndef WORKER_H
    #define WORKER_H
    
    #include <QObject>
    
    class Worker : public QObject
    {
        Q_OBJECT
    public:
        Worker(QObject* parent = 0);
        ~Worker();
    
    public slots:
        void work();
    
    signals:
        void finished();
    };
    
    #endif // WORKER_H
    
    #include "Worker.h"
    #include <QDebug>
    
    Worker::Worker(QObject* parent)
        : QObject(parent)
    {
    
    }
    
    Worker::~Worker()
    {
        qDebug() << "~Worker()";
    }
    
    void Worker::work()
    {
        static int i = 0;
    
        while (1)
        {
            qDebug() << i++;
            if (i == 2e4)
            {
                break;
            }
        }
        emit finished();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You have to wait on your thread to be finished in that case.

      QThread has such a method.

      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
      • M Offline
        M Offline
        mk33
        wrote on last edited by mk33
        #3

        Problem solved. Thank you.

            int ret = a.exec();
        
            workerThread->quit();
            workerThread->wait();
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Great !

          Then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Note that you may have to first select the "Ask as question" option and then you can mark the thread as solved.

          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

          • Login

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