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. Multiple object in QThread

Multiple object in QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 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.
  • S Offline
    S Offline
    s002wjh
    wrote on last edited by VRonin
    #1

    Can put multiple of object or duplicate object/Qwidget into Qthread?

    for example

    class test_class : public Qwidget
    {
    do something
    }
    
    void main(){
    Qthread *test_thread = new Qthread;
    for ( int n=0 ; n<5; n++{
    
     testobj[n] = new test_class;
     testobj[n]->moveToThread(test_thread);
     
    }
    
    thest_thread->start();
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Yes you can have multiple QObject moved to a QThread. Note that you can't call GUI related methods from a thread that is not the main thread.

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

        Can I move QWidget to a thread. Basically the QWidget in my class only set some value for Qpixmap but doesn't plot them. It plotted in different functions

        VRoninV mrjjM O 3 Replies Last reply
        0
        • S s002wjh

          Can I move QWidget to a thread. Basically the QWidget in my class only set some value for Qpixmap but doesn't plot them. It plotted in different functions

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @s002wjh said in Multiple object in QThread:

          Can I move QWidget to a thread

          No.

          From https://doc.qt.io/qt-5.10/thread-basics.html

          As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • S s002wjh

            Can I move QWidget to a thread. Basically the QWidget in my class only set some value for Qpixmap but doesn't plot them. It plotted in different functions

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @s002wjh
            Hi
            If you can inherit from QObject instead , then it would be better.

            1 Reply Last reply
            2
            • S s002wjh

              Can I move QWidget to a thread. Basically the QWidget in my class only set some value for Qpixmap but doesn't plot them. It plotted in different functions

              O Offline
              O Offline
              ollarch
              wrote on last edited by
              #6

              @s002wjh The QWidget and QPixmap are GUI classes so it's not permitted to modify them in another thread than the main thread.

              If you want to change some image pixels you can use QImage that is not a GUI class.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                s002wjh
                wrote on last edited by
                #7

                K tux.

                How do I pass dada array between main thread and worker thread. Using connect or can I set pointer. PointerWorkerobj->function(&Data) with mutex ?

                O 1 Reply Last reply
                0
                • S s002wjh

                  K tux.

                  How do I pass dada array between main thread and worker thread. Using connect or can I set pointer. PointerWorkerobj->function(&Data) with mutex ?

                  O Offline
                  O Offline
                  ollarch
                  wrote on last edited by
                  #8

                  @s002wjh It depens. You said that your worker thread will modify some data, so I recommend you to to protect the concurrent access with a Mutex and use a pointer to access the data. This will give you a better performance instead of making a copy of the data.

                  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