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.1k 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.
  • J Offline
    J Offline
    JKSH
    Moderators
    wrote on 26 Jul 2013, 23:50 last edited by
    #39

    An event loop is an infinite loop that processes signals and events. It waits for signals/events to arrive, then it calls functions to handle them.

    The main thread starts an event loop by calling QCoreApplication::exec():
    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    // ...

    // This line starts an (infinite) event loop, then
    // returns its status when the loop is exited
    return a.exec();
    

    }
    @

    QThread starts an event loop by calling QThread::exec():
    @
    void QThread::run()
    {
    // This line starts an (infinite) event loop, but
    // doesn't return an exit status value
    this->exec();
    }
    @

    Also, see "this page":http://qt-project.org/doc/qt-5.1/qtcore/threads-qobject.html

    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 29 Jul 2013, 05:00 last edited by
      #40

      Hii..gm.
      Thnx for ur important time and reply on my query.
      I am not relates the things.
      come again on our main point and please comfirm me that then when we you should subclass QThread class for thread then there is no EVENT LOOP?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on 29 Jul 2013, 05:28 last edited by
        #41

        Hi,

        At first, I don't think other can make you understand this in short word, if the documentation can not help you.

        But I think you can easily find with method is suitable when you writing your application.

        1. Subclass the QThread and re-implement the QThread::run()
        2. If you find that slots doesn't work correctly without moveToThread(this), then you are doing it wrong. Please use another method.
          3 Otherwise, you are doing it right.

        [quote author="Ak@sh" date="1375074051"]Hii..gm.
        Thnx for ur important time and reply on my query.
        I am not relates the things.
        come again on our main point and please comfirm me that then when we you should subclass QThread class for thread then there is no EVENT LOOP?[/quote]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Aksh
          wrote on 29 Jul 2013, 08:12 last edited by
          #42

          please can you give me one simple example or senario which shows clearly need of moveToThread(this) over subclass QThread...

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JKSH
            Moderators
            wrote on 29 Jul 2013, 08:29 last edited by
            #43

            [quote author="Ak@sh" date="1375085561"]please can you give me one simple example or senario which shows clearly need of moveToThread(this) over subclass QThread...[/quote]It's easier to do it the other way round. Please tell us what you want to do, and we'll explain which method you need.

            The experts told people to stop subclassing QThread because many people did it wrongly. It is easier to make mistakes when subclassing QThread. But, you are still allowed to subclass QThread.

            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 29 Jul 2013, 09:44 last edited by
              #44

              In my project from one hardware I have to receive data from serial and acording to updated data I have to update my GUI.Paralally user can perform some operations on GUI(like delete/add/update etc...).

              So In above my requirement 3 threads will work -

              1. main thread i.e. GUI
                2.thread which send request to H/w
                3.thread which continuously waiting for data to receive from H/w.
              1 Reply Last reply
              0
              • A Offline
                A Offline
                Aksh
                wrote on 29 Jul 2013, 10:14 last edited by
                #45

                Hi JKSH...I am waiting for your reply.
                If you have any doubtds so please askSo I can clear you.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 30 Jul 2013, 08:28 last edited by
                  #46
                  1. It sounds like you want to send requests when a user clicks on something in the GUI? If so, this thread needs to receive signals from your GUI so it needs an event loop. Use moveToThread() to put a worker QObject in the thread to handle the signals.

                  2. How do you receive data? The answer will determine if you need an event loop or not. (Classes like QTcpSocket and QSerialPort need an event loop to work. But if you use low-level code to poll your hardware, then you don't need an event loop)

                  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 30 Jul 2013, 13:20 last edited by
                    #47

                    Hi KJSH.....
                    thnx for your reply.
                    from serial line I have to recive data.
                    -Actualy I read data continuously from serial line and then put into system queue.
                    -and my next thread available to read system queue continuously so whatever data receive other thread can use it to update on GUI.

                    And all this I implemented by subclass QThread and override run() and all working well.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 31 Jul 2013, 15:26 last edited by
                      #48

                      If it is already working well, then don't change it :)

                      Usually, people discuss about "how should I implement ________?" before starting the project. If you need threads in the future, just remember: there are 2 ways to use QThread, and the "best" method depends on what you want to do.

                      • If you need to receive signals/events in the other thread, then don't subclass QThread.
                      • If you don't need receive signals/events in the other thread, then subclass QThread.

                      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 1 Aug 2013, 04:39 last edited by
                        #49

                        yes I agree with you but What I implemented is wrong then according to your two ways:

                        1. If you need to receive signals/events in the other thread, then don’t subclass QThread.
                        2. If you don’t need receive signals/events in the other thread, then subclass QThread.

                        Because I emits signals from my Thread which receiving data from Serial lines and those signals handled by other(GUI) thread.
                        and I subclass QThread in my implementation and now I feel its wrong implementation according to you.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JKSH
                          Moderators
                          wrote on 1 Aug 2013, 05:11 last edited by
                          #50

                          [quote author="Ak@sh" date="1375331989"]

                          1. If you need to receive signals/events in the other thread, then don’t subclass QThread.
                          2. If you don’t need receive signals/events in the other thread, then subclass QThread.

                          Because I emits signals from my Thread which receiving data from Serial lines and those signals handled by other(GUI) thread.
                          and I subclass QThread in my implementation and now I feel its wrong implementation according to you.[/quote]Don't worry, it is not wrong.

                          I said, if a thread receives signals, then that thread should not use a subclassed QThread. But, if the thread only emits signals (not receive), you can subclass QThread.

                          A thread needs an event loop to receive signals, but it doesn't need an event loop to emit signals.

                          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 1 Aug 2013, 05:32 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 1 Aug 2013, 06:02 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 1 Aug 2013, 06:05 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
                                • J Offline
                                  J Offline
                                  JKSH
                                  Moderators
                                  wrote on 1 Aug 2013, 06:47 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 1 Aug 2013, 07:07 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 1 Aug 2013, 07:17 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 2 Aug 2013, 08:41 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
                                        • J Offline
                                          J Offline
                                          JKSH
                                          Moderators
                                          wrote on 2 Aug 2013, 12:51 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

                                          48/64

                                          31 Jul 2013, 15:26

                                          • Login

                                          • Login or register to search.
                                          48 out of 64
                                          • First post
                                            48/64
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved