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. Question about Threads
Qt 6.11 is out! See what's new in the release blog

Question about Threads

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

    Hi,

    I have a question about using threads.

    Until know I derived my classes form QObject and then I made a thread and moved my instances to a thread like this:

    QThread* thread = new QThread;
    Worker* worker = new Worker();
    worker->moveToThread(thread);
    connect(thread, SIGNAL(started()), worker, SLOT(process()));
    connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
    

    I always emitted finished and used the thread only for the function which was connected with the start signal of the thread.

    Now I would like to start a thread which is running all the time while the GUI thread is running.
    The worker has several methods I would like to call from the GUI thread using signal slots.

    So would it work just to do it like this:

    QThread* thread = new QThread;
    Worker* worker = new Worker();
    worker->moveToThread(thread);
    thread->start();*/
    
    //Are the following methods executed in the thread of the worker object or in the GUI thread?
    worker->method1(); 
    worker->method2();
    worker->method3(); 
    

    If I would connect methods with the Signal-Slot-Mechanism without specifying a connection type QT would automatically use a Queued Connection, right?

    Thank you very much :-)

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2
      //Are the following methods executed in the thread of the worker object or in the GUI thread?
      worker->method1(); 
      worker->method2();
      worker->method3();
      

      All these calls will be executed in the thread calling them.

      I would suggest to provide the connection type explicitly, so you do not do any assumption about the type Qt selects by default.

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

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RolBri
        wrote on last edited by
        #3

        Thanks,

        this means if I use always signals and slots and specify the connection type to "QueuedConnection" the members would be executed in the worker thread which always runs in the example I provided?

        T 1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes, it should be like this

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

          1 Reply Last reply
          0
          • R RolBri

            Thanks,

            this means if I use always signals and slots and specify the connection type to "QueuedConnection" the members would be executed in the worker thread which always runs in the example I provided?

            T Offline
            T Offline
            t3685
            wrote on last edited by
            #5

            @RolBri

            As an alternative, you can use QMetaObject::invokeMethod instead of signals and slots. FYI

            1 Reply Last reply
            1
            • R Offline
              R Offline
              RolBri
              wrote on last edited by
              #6

              Thank you all very much :-)

              I tested both and both works fine.
              The QMetaObject::invokeMethod is really convenient :-)

              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