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. QThread with signals and slots
Forum Updated to NodeBB v4.3 + New Features

QThread with signals and slots

Scheduled Pinned Locked Moved General and Desktop
26 Posts 6 Posters 48.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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #6

    Thanks Andre, I'm trying your way now:

    I've created a (derived) instance of QObject (which includes a signal), connected the objects signal to my update slot (is that correct?)a nd I've used moveToThread. But how do I emit the signal from my thread, since MyThread doesn't have a signal at the moment?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ivan
      wrote on last edited by
      #7

      Well, signals/slots should work even without moveToThread().

      At least they do on posix threads (I've just tested).

      Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
      Qt Ambassador (from the old Nokia days)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #8

        It's a bit problematic if your objects are from diffrent threads

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #9

          [quote author="goocreations" date="1294519650"]Thanks Andre, I'm trying your way now:

          I've created a (derived) instance of QObject (which includes a signal), connected the objects signal to my update slot (is that correct?)a nd I've used moveToThread. But how do I emit the signal from my thread, since MyThread doesn't have a signal at the moment?[/quote]

          http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects :-)

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #10

            Thanks peppe, I went through all that, and tried it, but without any success

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #11

              The connect itself is fine, and if you've understood my article, specifying Qt::QueuedConnection is useless. Is an event loop running in the thread MyClass is living in?

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goocreations
                wrote on last edited by
                #12

                No, no event loop is running in MyClass

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dangelog
                  wrote on last edited by
                  #13

                  Not in "MyClass", in the thread your MyClass object is living in. If not, read the article and understand what's wrong.

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #14

                    Hi goocreations, queued connections need an event loop running in the thread that own sthe object that contains the slot. So if the creating thread is the main thread, has yout main thread an event loop?

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #15

                      [quote author="goocreations" date="1294519650"]Thanks Andre, I'm trying your way now:

                      I've created a (derived) instance of QObject (which includes a signal), connected the objects signal to my update slot (is that correct?)a nd I've used moveToThread. But how do I emit the signal from my thread, since MyThread doesn't have a signal at the moment?[/quote]
                      You start work in your thread by giving your worker object a slot. In the default implementation, your thread will get an event loop (run() calls exec()), so you can invoke a slot in it. You can invoke that slot by connecting a signal to it from the main thread, or by using QMetaObject::invokeMethod. In the latter case, don't forget to include the Qt::QueuedConnection flag, otherwise you make a direct method call and your slot won't be executed in the new threads' context, but in the main threads' context.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Satmosc
                        wrote on last edited by
                        #16

                        [quote author="Andre" date="1294519136"]The main problem is, that goocreations is using QThread in the wrong way. QThread is a class managing a thread. It is not the thread itself.

                        This is a frequently returning topic, mostly because of documentation issues in this respect in Qt. The recommended way of working with threads in Qt has changed since the documentation was
                        [quote author="Andre" date="1294572814"][quote author="goocreations" date="1294519650"]Thanks Andre, I'm trying your way now:

                        I've created a (derived) instance of QObject (which includes a signal), connected the objects signal to my update slot (is that correct?)a nd I've used moveToThread. But how do I emit the signal from my thread, since MyThread doesn't have a signal at the moment?[/quote]
                        You start work in your thread by giving your worker object a slot. In the default implementation, your thread will get an event loop (run() calls exec()), so you can invoke a slot in it. You can invoke that slot by connecting a signal to it from the main thread, or by using QMetaObject::invokeMethod. In the latter case, don't forget to include the Qt::QueuedConnection flag, otherwise you make a direct method call and your slot won't be executed in the new threads' context, but in the main threads' context.
                        [/quote]

                        written. It is usually better not to add signals, let alone slots, to QThread. Instead, create a QObject (derived) instance, and call moveToThread on it to move it to the thread. Put your signals and slots in this worker object instead.

                        See "this blog":http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ for details.[/quote]

                        Hi , I was reading this old post , Then I was surprised after reading that documents are old and not correct way.
                        I read several post in Qt blog , with different Idea.... what is really the correct way ?
                        I am also confused now , what is the correct way of using movetothread !!!!

                        by the way , I always use movetothread and I use signal and slots and I never had problem but I prefer to be on the correct way on implementation

                        thanks

                        M.Hatami
                        To the Rationalism

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #17

                          Reference the blog and "this":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects wiki entry. Only based on the official Qt documentation, it is way too easy to get it wrong. Also, do not, please, use moveToThread(this) inside your QThread subclass. You probably don't understand what that does exactly, and what the consequences are.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Satmosc
                            wrote on last edited by
                            #18

                            Thank you very much. You are right. I am a bit confused now about movetothread while I know it is used later in the thread for events and ...

                            I need to get help by reading a source code which has correct implementation.
                            As I remember in the documents , ::Start() should not be called from the thread itself !!! while I see in the code "queuedcustomtype" (attached in the Qt Sources) the start has been called from a method inside the Thread Subclass !!!!! ?

                            M.Hatami
                            To the Rationalism

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dangelog
                              wrote on last edited by
                              #19

                              [quote]I need to get help by reading a source code which has correct implementation. As I remember in the documents , ::Start() should not be called from the thread itself ![/quote]

                              What do you mean with that? It's supposed to be called from the thread your thread object is living in. Calling a custom method foo() which in turns calls start() is perfectly fine.

                              Software Engineer
                              KDAB (UK) Ltd., a KDAB Group company

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #20

                                Copy/paste from the mentioned wiki entry:

                                @
                                class Worker : public QObject
                                {
                                Q_OBJECT

                                public slots:
                                void doWork() {
                                /* ... */
                                }
                                };

                                /* ... */
                                QThread *thread = new QThread;
                                Worker *worker = new Worker;
                                connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
                                worker->moveToThread(thread);
                                thread->start();
                                @

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Satmosc
                                  wrote on last edited by
                                  #21

                                  [quote author="Andre" date="1306745564"]Copy/paste from the mentioned wiki entry:

                                  @
                                  class Worker : public QObject
                                  {
                                  Q_OBJECT

                                  public slots:
                                  void doWork() {
                                  /* ... */
                                  }
                                  };

                                  /* ... */
                                  QThread *thread = new QThread;
                                  Worker *worker = new Worker;
                                  connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
                                  worker->moveToThread(thread);
                                  thread->start();
                                  @
                                  [/quote]

                                  Thank you very much. so , it means SubClassing is already over since Qt 4.4(which it is not a Abstract class anymore) . so , I should use thread always this way !?
                                  by the way , the Qt wiki looks excelent .

                                  @peppe : in that example from Qt Sources I see that the thread::Start has been called from thread subclass and not the thread that object lives in.

                                  M.Hatami
                                  To the Rationalism

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #22

                                    It is not impossible to subclass QThread and use it that way, but it is no longer the recommended way to do things. I won't say you should always use the create-a-worker-QObject-and-move-it-to-a-vanilla-QThread method, but in general, yes, that is the way to go. Especially if you plan to start using signals and slots.

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      Satmosc
                                      wrote on last edited by
                                      #23

                                      @Andre: Thanks for information. yes , I guess there are situation that we may need to create new class from Qthread to add or reimplement functions.

                                      M.Hatami
                                      To the Rationalism

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        dangelog
                                        wrote on last edited by
                                        #24

                                        [quote author="Satmosc" date="1306746483"]
                                        @peppe : in that example from Qt Sources I see that the thread::Start has been called from thread subclass and not the thread that object lives in.
                                        [/quote]

                                        What's the example you're talking about? And "from the thread subclass" doesn't mean much; as I said, adding a method which in turn calls start() and calling that method is fine (and I doubt start() is getting called from inside run()).

                                        Software Engineer
                                        KDAB (UK) Ltd., a KDAB Group company

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          Satmosc
                                          wrote on last edited by
                                          #25

                                          peppe: Check examples from QtSources Threading/queuedcustomtype
                                          and in file "renderthread.cpp", Line 66:

                                          @//![processing the image (start)]
                                          void RenderThread::processImage(const QImage &image)
                                          {
                                          if (image.isNull())
                                          return;

                                          m_image = image;
                                          m_abort = false;
                                          start();
                                          

                                          }@

                                          M.Hatami
                                          To the Rationalism

                                          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