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 and workers classes arguments
QtWS25 Last Chance

Qthread and workers classes arguments

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.1k Views
  • 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.
  • D Offline
    D Offline
    dedetuga
    wrote on last edited by
    #1

    hi, I'm having some bad time trying understand how to use qthreads,
    I have some code to be runned in a parallel thread, for that I used Qthread with classes, for that I used this page as sample QThreads general usage .
    I took a wile to understand how implement this code is my first time implementing threads in codding.
    I'm managed to put the code running in the thread and worker but I need to pass some data from variables to the worker. I can't find any complete sample of "how to do it" I only find some brief explanations with code fragments and I'm not able to understand.

    how can I do that?

    Thanks

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

      Solved!!!!!!!!!!!!!!!!!!!!!

      I miss a default value...I replace:

      explicit Worker(QObject *parent = nullptr, int variable);
      

      by:

       explicit Worker(QObject *parent = nullptr, int variable = 0);
      

      And everything start working, there is my sample code for future references to noobs like me.

      mainwindow.cpp

              QThread* threadtest = new QThread;
              Worker* worker = new Worker(nullptr, 100);
      
              worker->moveToThread(threadtest);
              connect(worker, SIGNAL (finished()), threadtest, SLOT (quit()));
              connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
              connect(threadtest, SIGNAL (finished()), threadtest, SLOT (deleteLater()));
      
              threadtest->start();
      

      worker.cpp

      Worker::Worker(QObject *parent, int variable) : QObject(parent)
      {
          int intvar = variable;
      }
      

      worker.h

      class Worker : public QObject
      {
          Q_OBJECT
      public:
          explicit Worker(QObject *parent = nullptr, int variable = 0);
      ...
      ...
      ...
      
      1 Reply Last reply
      0
      • D dedetuga

        hi, I'm having some bad time trying understand how to use qthreads,
        I have some code to be runned in a parallel thread, for that I used Qthread with classes, for that I used this page as sample QThreads general usage .
        I took a wile to understand how implement this code is my first time implementing threads in codding.
        I'm managed to put the code running in the thread and worker but I need to pass some data from variables to the worker. I can't find any complete sample of "how to do it" I only find some brief explanations with code fragments and I'm not able to understand.

        how can I do that?

        Thanks

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @dedetuga When exactly do you need to pass parameters?
        You can simply pass the parameters when you create your worker:

        MyWorker *worker = new MyWorker(param1, param2, ...);
        

        Or you can pass them later using signals/slots.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        2
        • jsulmJ jsulm

          @dedetuga When exactly do you need to pass parameters?
          You can simply pass the parameters when you create your worker:

          MyWorker *worker = new MyWorker(param1, param2, ...);
          

          Or you can pass them later using signals/slots.

          D Offline
          D Offline
          dedetuga
          wrote on last edited by dedetuga
          #3

          Hi @jsulm, thanks for reply!
          I can pass the parameters any time, but may for now its better pass it at the start, following your sample...if i pass the parameter when creating my worker how do i receive them?

          in mainwindow.cpp

          ...
          QThread* threadtest = new QThread;
          MyWorker *worker = new MyWorker(param1, param2, ...);
          ...
          

          in worker.cpp

          ...
          Worker::Worker(QObject *parent) : QObject(parent)
          { 
             
          }
          ...
          

          in worker.h

          ...
          public:
              explicit Worker(QObject *parent = nullptr);
          ...
          

          I Have no idea how to handle the parameters in worker.cpp and worker.h files to be able to receive them.
          How can I do it?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            You have to declare a constructor that takes these parameters and not only the "QObject *parent".

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              You have to declare a constructor that takes these parameters and not only the "QObject *parent".

              D Offline
              D Offline
              dedetuga
              wrote on last edited by
              #5

              hi, @SGaist, thanks for reply!

              Can you please sample some code? I already try but I don't know how to do it and i end every time with errors.

              thanks

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                What did you try ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dedetuga
                  wrote on last edited by
                  #7

                  main:

                  Worker* worker = new Worker(nullptr, 10);
                  

                  w.cpp

                  Worker::Worker(QObject *parent, int variable) : QObject(parent)
                  

                  w.h

                      explicit Worker(QObject *parent = nullptr, int variable);
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    You should put the parent parameter as last with the usual default value.

                    Otherwise, that's correct.

                    What error do you get ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                      Solved!!!!!!!!!!!!!!!!!!!!!

                      I miss a default value...I replace:

                      explicit Worker(QObject *parent = nullptr, int variable);
                      

                      by:

                       explicit Worker(QObject *parent = nullptr, int variable = 0);
                      

                      And everything start working, there is my sample code for future references to noobs like me.

                      mainwindow.cpp

                              QThread* threadtest = new QThread;
                              Worker* worker = new Worker(nullptr, 100);
                      
                              worker->moveToThread(threadtest);
                              connect(worker, SIGNAL (finished()), threadtest, SLOT (quit()));
                              connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
                              connect(threadtest, SIGNAL (finished()), threadtest, SLOT (deleteLater()));
                      
                              threadtest->start();
                      

                      worker.cpp

                      Worker::Worker(QObject *parent, int variable) : QObject(parent)
                      {
                          int intvar = variable;
                      }
                      

                      worker.h

                      class Worker : public QObject
                      {
                          Q_OBJECT
                      public:
                          explicit Worker(QObject *parent = nullptr, int variable = 0);
                      ...
                      ...
                      ...
                      
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        As I already suggested, put parent last. You can have all the parameters without default values before the first that has one.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        D 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          As I already suggested, put parent last. You can have all the parameters without default values before the first that has one.

                          D Offline
                          D Offline
                          dedetuga
                          wrote on last edited by
                          #11

                          @SGaist thanks, that worked and simplify the code. thanks for help @SGaist and @jsulm

                          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