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.7k 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
    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
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #26

                      That processImage method gets called from the right thread (in the example, the GUI thread, which is where the RenderThread object is living), so it's safe to use that way.

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

                      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