Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qthread and workers classes arguments



  • 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



  • 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);
    ...
    ...
    ...
    

  • Lifetime Qt Champion

    @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.



  • 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?


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

    What did you try ?



  • 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);
    

  • Lifetime Qt Champion

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

    Otherwise, that's correct.

    What error do you get ?



  • 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);
    ...
    ...
    ...
    

  • Lifetime Qt Champion

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



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


Log in to reply