parameter passing to Qthread
-
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 ....??
-
@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? -
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.
-
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 -
@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. -
@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