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. Discussion about threads, moveToThread etc.
Forum Updated to NodeBB v4.3 + New Features

Discussion about threads, moveToThread etc.

Scheduled Pinned Locked Moved General and Desktop
64 Posts 8 Posters 32.5k Views 1 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.
  • A Offline
    A Offline
    Aksh
    wrote on last edited by
    #51

    Ok my one thread(A) is emits signals but other thread(B) receiving signals and handle them there.
    So ultimately other thread(B) receiving signals right?
    and I subclass QThread in that thread(B) which receiving signals which emits by first thread(A).

    Now I did wrong right?????????????

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #52

      Yes, you did it wrong.

      Keep in mind that, QThread::run() is the entry point of the thread, which more or less like the main() is the entry point of the process.If you want some code to be run in the thread, they must be called inside the QThread:;run() function.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #53

        If you want to understand in which way the queued-connection works, and in which way the slots get called in the QThread::run(). You at least need to make yourself familiar with Event Loop.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #54

          [quote author="Ak@sh" date="1375335153"]Ok my one thread(A) is emits signals but other thread(B) receiving signals and handle them there.
          So ultimately other thread(B) receiving signals right?[/quote]Correct.

          [quote]and I subclass QThread in that thread(B) which receiving signals which emits by first thread(A).

          Now I did wrong right????????????? [/quote]Correct. In this scenario, (B) should not be subclassed.

          Remember: the QThread is an object that controls the thread, but the QThread object is not a thread. If you connect a signal to a QThread's slot, the slot will probably run in the "wrong" thread.

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aksh
            wrote on last edited by
            #55

            Hey its really great thing that only I want to asked and confirm with you.
            "If you connect a signal to a QThread’s slot, the slot will probably run in the “wrong” thread."

            can you please give me any live example or scenario from all this will more clear for me.
            Please sir reply

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #56

              Hi, QThread is subclass of QObject. So "you connect a signal to a QThread’s slot" == "you connect a signal to a normal QObject’s slot"

              There is no other magic here.
              [quote author="Ak@sh" date="1375340827"]Hey its really great thing that only I want to asked and confirm with you.
              "If you connect a signal to a QThread’s slot, the slot will probably run in the “wrong” thread."

              can you please give me any live example or scenario from all this will more clear for me.
              Please sir reply [/quote]

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Aksh
                wrote on last edited by
                #57

                can you please give me any live example or scenario from all this will more clear for me.

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #58

                  Remember these two things all the time:

                  A QObject lives in the thread that created it

                  A QObject's slots will run in the thread that it lives in

                  Example of a bad class:
                  @
                  class BadThread : public QThread {
                  Q_OBJECT

                  public:
                  BadThread(QObject *parent = 0);

                  public slots:
                  void badSlot();

                  protected:
                  void run();
                  };
                  @

                  Example of bad code using the bad class:
                  @
                  int main(...){
                  ...

                  // Create 2 objects (myObject and badThread),
                  // which both live in the main thread.
                  MyObject *myObject = new MyObject();    
                  BadThread *badThread = new BadThread();
                  
                  // Make a signal-slot connection
                  QObject::connect(myObject, SIGNAL(mySignal()),
                          badThread, SLOT(badSlot()));
                  
                  // Start the new thread.
                  // BadThread::run() runs in the new thread.
                  badThread->start();
                  
                  // Emit a signal to invoke the slot.
                  // BadThread::badSlot() runs in the main thread.
                  emit mySignal();    
                  
                  ...
                  

                  }
                  @

                  In the above example:

                  badThread was created by the main thread, so it lives in the main thread

                  badThread lives in the main thread, so its slots run in the main thread.

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Aksh
                    wrote on last edited by
                    #59

                    Ok.........Finaly I think I got it.
                    All summary of our Discussion :
                    tow ways to use QThread -

                    1. create an object of QThread class and call function - moveToThread().
                      2.subclass QThread class and override function - run()

                    when you use moveToThread()? : -
                    if you want to run any SLOT as thread of any class then you should use moveToThread().

                    when you use run()? : -
                    if you know u want to run only(or very specificaly) run() of any class and not other SLOTS of same class which having that run() then you should use subclass QThread and override run().

                    If you think still I am going wrong in understanding of QThread then please correct me sir...

                    Thnax for your reply and guidance...

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Aksh
                      wrote on last edited by
                      #60

                      Hi JKSH........

                      r u thr?

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #61

                        Hi Ak@sh, yes your summary is good :)

                        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
                          dbzhang800
                          wrote on last edited by
                          #62

                          Hi, I have write a blog about this, may be you have interest to it.

                          http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Aksh
                            wrote on last edited by
                            #63

                            Thanx JKSH.>!!!

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Aksh
                              wrote on last edited by
                              #64

                              hi JKSH.

                              I found one error:-

                              undefined symbol _zn9qlistdata11detach_growepii qt

                              I am trying to run my application which compiled on different machine(but OS and Qt version is same).

                              Please give me any solution

                              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