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. My tutorial on how to properly use QThreads
Forum Updated to NodeBB v4.3 + New Features

My tutorial on how to properly use QThreads

Scheduled Pinned Locked Moved General and Desktop
34 Posts 14 Posters 45.5k Views 5 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.
  • M Offline
    M Offline
    MayaPosch
    wrote on 29 Feb 2012, 21:54 last edited by
    #21

    http://developer.qt.nokia.com/wiki/QThreads_general_usage

    There you go... feel free to link to it from other articles in an appropriate manner :)

    Feedback is also very welcome.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BelenMuñoz
      wrote on 30 Jan 2013, 11:31 last edited by
      #22

      Thank you very much Maya, this tutorial has been very useful for my project!!

      Regards.

      Me casé con un enano pa jartarme de reí.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MayaPosch
        wrote on 30 Jan 2013, 11:34 last edited by
        #23

        You're quite welcome, BelenMuñoz :)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on 10 Mar 2013, 09:40 last edited by
          #24

          Thanks for such a nice article, made the picture more clear about the implementation.

          Regards

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jigar.patel
            wrote on 19 Sept 2013, 15:16 last edited by
            #25

            But what about child qobjects (heap objects)?
            By this thread creation method, child objects are not moved in to thread.
            how can i move them into thread?
            please help me.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JKSH
              Moderators
              wrote on 20 Sept 2013, 02:02 last edited by
              #26

              Hi,

              When you move a parent QObject, all its children are automatically moved too.

              Remember though that member variables do not automatically become children; the parent-child relationship must be set by either:

              • passing the parent's pointer into the child's constructor, or
              • calling QObject::setParent()

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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jigar.patel
                wrote on 20 Sept 2013, 03:11 last edited by
                #27

                class SecondaryOperation: public QObject
                {
                Q_OBJECT
                private:
                bool printtype;
                QTimer *tt;
                public:
                SecondaryOperation(bool);
                public slots:
                void runsecondaryfunction();
                void ttevent();
                signals:
                void taskfinish();
                };

                i am creating one object of above class in main thread. i am also creating one thread, to which i am passing "runsecondaryfunction()" public slot. in "runsecondaryfunction()" there is one while loop, that will continuous run in thread. i have initialized timer tt in class constructor. timeout event of tt timer is linked with "ttevent()". but there is no timeout event occur. also if i am declaring other seperate timer object in "runsecondaryfunction()", program stop unexpectedly. also i want to use one object of qnetworkaccessmanager in SecondaryOperation class. then how can i use it?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 20 Sept 2013, 04:12 last edited by
                  #28

                  Hi jigar.patel,

                  There are many parts in your question. Please "start a new discussion":http://qt-project.org/forums/newtopic/10/ and show more of your code (especially the implementation of runsecondaryfunction() and ttevent()) -- your descriptions don't have enough details.

                  Also, please put '@' before and after your code to give it proper highlighting. That makes it easier to read.

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

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    deleted28
                    wrote on 6 Jul 2015, 15:56 last edited by deleted28 7 Jun 2015, 16:16
                    #29

                    Hello Maya,

                    beginner question on this sentence "... you should NEVER allocate heap objects (using new) in the constructor of the QObject class"
                    I do not undestand which is the constructor of the QObject class.

                    This constructor ? : Worker::Worker() {} ?

                    Can i do this way, or is this the constructor meant above ?

                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        QThread* thread = new QThread;
                        Worker* worker = new Worker();
                        worker->moveToThread(thread);
                    

                    If this is ok ( and it works in this simple version) where not to never allocate ... ?
                    thank you

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 12 Jul 2015, 23:54 last edited by
                      #30

                      as far as I can read, she means in the worker constructor as we later do

                      Worker* worker = new Worker();
                      worker->moveToThread(thread);

                      so if we in Worker constructor create new other objects,
                      they are not moved and stays in main thread.

                      J 1 Reply Last reply 13 Jul 2015, 05:21
                      0
                      • mrjjM mrjj
                        12 Jul 2015, 23:54

                        as far as I can read, she means in the worker constructor as we later do

                        Worker* worker = new Worker();
                        worker->moveToThread(thread);

                        so if we in Worker constructor create new other objects,
                        they are not moved and stays in main thread.

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 13 Jul 2015, 05:21 last edited by
                        #31

                        @mrjj said:

                        so if we in Worker constructor create new other objects,
                        they are not moved and stays in main thread.

                        Only if the other objects aren't parented properly. See the QObject documentation:

                        Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.

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

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          deleted28
                          wrote on 13 Jul 2015, 05:25 last edited by
                          #32

                          Hello mrjj,

                          if I see this line in the Worker constructor, i tend to assume the code would create a neverending number of Worker objects. For me this looks recursive, (?)
                          That's why I asked what Maya meant by NEVER do ... but "what ?"

                          Worker::Worker(QObject *parent) : QObject(parent)
                          {
                          Worker* worker = new Worker();
                          ...
                          }
                          

                          In which point my thinking is bad ?

                          K mrjjM 2 Replies Last reply 13 Jul 2015, 07:53
                          0
                          • D deleted28
                            13 Jul 2015, 05:25

                            Hello mrjj,

                            if I see this line in the Worker constructor, i tend to assume the code would create a neverending number of Worker objects. For me this looks recursive, (?)
                            That's why I asked what Maya meant by NEVER do ... but "what ?"

                            Worker::Worker(QObject *parent) : QObject(parent)
                            {
                            Worker* worker = new Worker();
                            ...
                            }
                            

                            In which point my thinking is bad ?

                            K Offline
                            K Offline
                            koahnig
                            wrote on 13 Jul 2015, 07:53 last edited by
                            #33

                            @wally123

                            Creating an instance of the same object within the constructor is always deadly. That has nothing to do with QThreads.

                            @JKSH said:

                            Only if the other objects aren't parented properly. See the QObject documentation:

                            Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.

                            That is a good point.

                            Vote the answer(s) that helped you to solve your issue(s)

                            1 Reply Last reply
                            0
                            • D deleted28
                              13 Jul 2015, 05:25

                              Hello mrjj,

                              if I see this line in the Worker constructor, i tend to assume the code would create a neverending number of Worker objects. For me this looks recursive, (?)
                              That's why I asked what Maya meant by NEVER do ... but "what ?"

                              Worker::Worker(QObject *parent) : QObject(parent)
                              {
                              Worker* worker = new Worker();
                              ...
                              }
                              

                              In which point my thinking is bad ?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 13 Jul 2015, 09:07 last edited by
                              #34

                              @wally123
                              As I read it, she meant other objects

                              Worker::Worker(QObject *parent) : QObject(parent)
                              {
                              SomeWidget*  MyWid= new SomeWidget();
                              }
                              

                              and as JKSH then teach us is that you -can- do that if you then also do

                              MyWid->setParent(this)

                              I dont think she warned about

                              Worker::Worker(QObject *parent) : QObject(parent)
                              {
                              Worker* worker = new Worker();
                              }
                              

                              And yes, this will be recursive to the point of blowing up.

                              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