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. parameter passing to Qthread
QtWS25 Last Chance

parameter passing to Qthread

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadqthread threadsc++11qtcreator 5.0qt5
7 Posts 4 Posters 4.6k 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.
  • V Offline
    V Offline
    Vivek_A
    wrote on 20 Jan 2022, 06:23 last edited by
    #1

    HI,
    With the reference from link1 link2.I implimented a simple thread for passing parameters to thread Run but its not working where is the mistake????
    code:

    header file:
    class myThread : public QThread
    {
      Q_OBJECT
    
    public:
        myThread(QObject *parent = 0,int variable = 0);
        void run(int variable);
    
    signals:
        void threadSignal1();
    };
    
    cpp file :
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        onethread = new myThread(this,100);
      
        connect(onethread, SIGNAL(threadSignal1()),
                    this, SLOT(mySlot1()));
        
    
        onethread->start(QThread::NormalPriority);
    }
    
    myThread::myThread(QObject *parent,int variable)
        : QThread(parent)
    {
    }
    
    void myThread::run(int variable)
    {
            qDebug("thread one----------");
             qDebug()<<variable;
            usleep(100000);
    }
    

    No error , Gui console opening , nothing showing editor debug ....??

    vk

    J C 2 Replies Last reply 20 Jan 2022, 06:26
    0
    • V Offline
      V Offline
      Vivek_A
      wrote on 20 Jan 2022, 11:00 last edited by
      #7

      @jsulm @Christian-Ehrlicher signal/slot method working

          connect(this,&MainWindow::starting,onethread,&myThread::recvNum);
          onethread->start();
       for(int i=1;i<5;i++)
          
              {
          
                  emit starting(i);
          
              }

      vk

      1 Reply Last reply
      1
      • V Vivek_A
        20 Jan 2022, 06:23

        HI,
        With the reference from link1 link2.I implimented a simple thread for passing parameters to thread Run but its not working where is the mistake????
        code:

        header file:
        class myThread : public QThread
        {
          Q_OBJECT
        
        public:
            myThread(QObject *parent = 0,int variable = 0);
            void run(int variable);
        
        signals:
            void threadSignal1();
        };
        
        cpp file :
        
        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            onethread = new myThread(this,100);
          
            connect(onethread, SIGNAL(threadSignal1()),
                        this, SLOT(mySlot1()));
            
        
            onethread->start(QThread::NormalPriority);
        }
        
        myThread::myThread(QObject *parent,int variable)
            : QThread(parent)
        {
        }
        
        void myThread::run(int variable)
        {
                qDebug("thread one----------");
                 qDebug()<<variable;
                usleep(100000);
        }
        

        No error , Gui console opening , nothing showing editor debug ....??

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 20 Jan 2022, 06:26 last edited by
        #2

        @Vivek_A said in parameter passing to Qthread:

        void run(int variable);

        This can't work this way!
        You are overloading run(), not overriding. Your run(int) will not be called as Qt does not know anything about it, the normal one will be called.
        Why don't you simply pass parameters to the myThread constructor?

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

        1 Reply Last reply
        0
        • V Vivek_A
          20 Jan 2022, 06:23

          HI,
          With the reference from link1 link2.I implimented a simple thread for passing parameters to thread Run but its not working where is the mistake????
          code:

          header file:
          class myThread : public QThread
          {
            Q_OBJECT
          
          public:
              myThread(QObject *parent = 0,int variable = 0);
              void run(int variable);
          
          signals:
              void threadSignal1();
          };
          
          cpp file :
          
          MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              onethread = new myThread(this,100);
            
              connect(onethread, SIGNAL(threadSignal1()),
                          this, SLOT(mySlot1()));
              
          
              onethread->start(QThread::NormalPriority);
          }
          
          myThread::myThread(QObject *parent,int variable)
              : QThread(parent)
          {
          }
          
          void myThread::run(int variable)
          {
                  qDebug("thread one----------");
                   qDebug()<<variable;
                  usleep(100000);
          }
          

          No error , Gui console opening , nothing showing editor debug ....??

          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 20 Jan 2022, 06:26 last edited by
          #3

          You can not pass a variable to run() - run is a virtual function without any parameters. Use the override keyword to catch such errors.

          You can create a setter function in your class derived from QThread where you can set the variable from outside before you start the thread.

          Also please use the new signal and slot syntax.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • V Offline
            V Offline
            Vivek_A
            wrote on 20 Jan 2022, 07:02 last edited by Vivek_A
            #4

            @jsulm @Christian-Ehrlicher

            onethread = new myThread(this,100);
            
            myThread::myThread(QObject *parent,int val)
               : QThread(parent)
            {
               qDebug()<<val;
            }
            This gives output 100 , success ,
            
            

            But onethread = new myThread(this,100); this will call once right?? what to do when i need to send continously in a loop,
            i found one example Github is that ok to follow

            vk

            J J 2 Replies Last reply 20 Jan 2022, 07:15
            0
            • V Vivek_A
              20 Jan 2022, 07:02

              @jsulm @Christian-Ehrlicher

              onethread = new myThread(this,100);
              
              myThread::myThread(QObject *parent,int val)
                 : QThread(parent)
              {
                 qDebug()<<val;
              }
              This gives output 100 , success ,
              
              

              But onethread = new myThread(this,100); this will call once right?? what to do when i need to send continously in a loop,
              i found one example Github is that ok to follow

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 20 Jan 2022, 07:15 last edited by
              #5

              @Vivek_A said in parameter passing to Qthread:

              what to do when i need to send continously in a loop

              Send what? Parameter?
              If so then do what @Christian-Ehrlicher suggested or use signals/slots to exchange data between threads.

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

              1 Reply Last reply
              0
              • V Vivek_A
                20 Jan 2022, 07:02

                @jsulm @Christian-Ehrlicher

                onethread = new myThread(this,100);
                
                myThread::myThread(QObject *parent,int val)
                   : QThread(parent)
                {
                   qDebug()<<val;
                }
                This gives output 100 , success ,
                
                

                But onethread = new myThread(this,100); this will call once right?? what to do when i need to send continously in a loop,
                i found one example Github is that ok to follow

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 20 Jan 2022, 07:16 last edited by
                #6

                @Vivek_A let me redirect you to my threading example, so that you don't have to use examples made with Qt4!

                https://github.com/DeiVadder/QtThreadExample

                it covers all Qt ways of doing something in parallel. In your case I would throw away the subclassing QThread approach and go for the WorkerObject approach.

                It the the WorkerObject subproject of the linked project


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2
                • V Offline
                  V Offline
                  Vivek_A
                  wrote on 20 Jan 2022, 11:00 last edited by
                  #7

                  @jsulm @Christian-Ehrlicher signal/slot method working

                      connect(this,&MainWindow::starting,onethread,&myThread::recvNum);
                      onethread->start();
                   for(int i=1;i<5;i++)
                      
                          {
                      
                              emit starting(i);
                      
                          }

                  vk

                  1 Reply Last reply
                  1

                  5/7

                  20 Jan 2022, 07:15

                  • Login

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