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. QThread in main-function

QThread in main-function

Scheduled Pinned Locked Moved General and Desktop
qthreadmain
3 Posts 2 Posters 2.8k 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    Hi,
    i am not quite sure if it is allowed to create a QThread in the main/entry-function.
    My code looks like the following schema, all works fine, but i'm not sure if it's just coincidence.

    #include <QtWidgets/QApplication>
    #include <QThread>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    
    	MyObject workerObject; // inherits from QObject
    
    	QThread workerThread;
    	workerObject.moveToThread(&workerThread);
    	workerThread.start();
    
    	MyMainWindow window(workerObject);  // create connections UI <-> workerObject in the constructor
    	window.show();
    
    	return a.exec();
    }
    

    Or should i create the QThread and my WorkerObject in the MyMainWindow class?

    Thanks!

    kshegunovK 1 Reply Last reply
    0
    • beeckscheB beecksche

      Hi,
      i am not quite sure if it is allowed to create a QThread in the main/entry-function.
      My code looks like the following schema, all works fine, but i'm not sure if it's just coincidence.

      #include <QtWidgets/QApplication>
      #include <QThread>
      
      int main(int argc, char *argv[])
      {
      	QApplication a(argc, argv);
      
      	MyObject workerObject; // inherits from QObject
      
      	QThread workerThread;
      	workerObject.moveToThread(&workerThread);
      	workerThread.start();
      
      	MyMainWindow window(workerObject);  // create connections UI <-> workerObject in the constructor
      	window.show();
      
      	return a.exec();
      }
      

      Or should i create the QThread and my WorkerObject in the MyMainWindow class?

      Thanks!

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

      @beecksche
      It's quite fine, but you should create the worker object in the heap and connect the thread's finished signal to the worker object's deleteLater slot. Wherever you create the actual QThread instance and the worker object is of no consequence, as all the widgets are running on top of the main() stack frame (in the same thread) anyway.

      PS.
      Also, if I were you I'd connect the aboutToQuit signal from the application object to post the quit event and wait for the thread to finish. Something along the lines:

      int main (int argc, char ** argv)
      {
          QApplication app(argc, argv);
      
          MyObject * workerObject = new MyObject;
      
          QThread workerThread;
          workerThread.start();
      
          workerObject->moveToThread(&workerThread);
      
          QObject::connect(&workerThread, SIGNAL(finished()), workerObject, SLOT(deleteLater()));
          QObject::connect(&app, SIGNAL(aboutToQuit()), &workerThread, SLOT(quit()));
      
          // ...
      
          int status = QApplication::exec();
          workerThread.wait(5000); // Wait up to 5 seconds for the worker to wrap things up, if it doesn't, just exit
      
          return status;
      }
      

      There are better and more sophisticated ways to wait for the worker, but this is simple and effective enough.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      5
      • beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by
        #3

        Thanks for your reply and help!

        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