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); ... ... ...
-
-
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? -
Hi,
You have to declare a constructor that takes these parameters and not only the "QObject *parent".
-
What did you try ?
-
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); ... ... ...
-
As I already suggested, put parent last. You can have all the parameters without default values before the first that has one.