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. How to pass additional argument to doWork() Thread
Forum Updated to NodeBB v4.3 + New Features

How to pass additional argument to doWork() Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 468 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.
  • S Offline
    S Offline
    sigmind
    wrote on 6 Jan 2020, 07:44 last edited by sigmind 1 Jun 2020, 07:47
    #1

    I have a simple multithreaded program to perform certain tasks in doWork() function.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        thread = new QThread();
        worker = new Worker();
    
        worker->moveToThread(thread);
        connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
        connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
        connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
    
    
        while(1)
        {
           worker->abort();
        //    thread->wait(); 
    
            worker->requestWork();
        }
    }
    

    The fact is I need to pass an additional argument to doWork() function to work with it. I can't find any way to pass the parameter to doWork() function.

    My doWork function should be like:

    void doWork(int x, Qstring name)
    
    J 1 Reply Last reply 6 Jan 2020, 08:28
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Jan 2020, 08:13 last edited by mrjj 1 Jun 2020, 09:53
      #2

      Hi
      Just make the worker object accept them in another way.
      Worker->setData(46,"sigmind");
      as the started() signal from the thread, won't have any way to give DoWork any parameters anyway.
      (not in a dynamic manner)

      1 Reply Last reply
      1
      • S sigmind
        6 Jan 2020, 07:44

        I have a simple multithreaded program to perform certain tasks in doWork() function.

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            thread = new QThread();
            worker = new Worker();
        
            worker->moveToThread(thread);
            connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
            connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
            connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
            connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
        
        
            while(1)
            {
               worker->abort();
            //    thread->wait(); 
        
                worker->requestWork();
            }
        }
        

        The fact is I need to pass an additional argument to doWork() function to work with it. I can't find any way to pass the parameter to doWork() function.

        My doWork function should be like:

        void doWork(int x, Qstring name)
        
        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 6 Jan 2020, 08:28 last edited by J.Hilk 1 Jun 2020, 08:32
        #3

        @sigmind since you're using qt 4 syntax, why don't you define a custom signal with default arguments ?

        //worker.h
        
        signals:
            void startSignal(int defaultInt = 0, QString defaultString = "default");
        
        //----
        connect(thread, SIGNAL(started()), worker, SIGNAL(startSignal()));
        connect(worker, SIGNAL(startSignal(int, QString)), worker, SLOT(doWork(int, QString)));
        

        Well, I'm still in holiday mode.
        Ignore that, simply give your doWork slot default arguments and you can connect it directly to the started signal.


        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
          VRonin
          wrote on 6 Jan 2020, 09:30 last edited by
          #4

          if the arguments are passed by value and known at the time of connection then you can also use
          connect(thread, &QThreadstarted, worker, std::bind(&Worker::doWork,worker,2,QStringLiteral("hello")));

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          1/4

          6 Jan 2020, 07:44

          • Login

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