Problem with interthread signals
-
Hi,
Can anyone please take a look at it. Here is a code snippet.
I have a Worker object placed into a worker thread. I try to do inter-thread signalling with this object from the main thread (MyWidget)..
The initialization is done in MyWidget's constructor. There I move the worker object into the worker thread, connect worker's error() signal into MyWidget's clError() slot, connect MyWidget's start() signal into the worker's run() slot, than I start the thread and emit a start() signal.
The worker's run() slot gets called and from there I emit an error() signal, but the connected MyWidget::clError() slot is not getting called.
Here is a console output:
Starting /home/endre/Prg/QtCreator/build-Test1-Desktop-Default/Test1...
ctor
run()
emit Error opening 'kernels/all.cl'
/home/endre/Prg/QtCreator/build-Test1-Desktop-Default/Test1 exited with code 0Why is the MyWidget::clError() slot not called?
-
hi
Did you check the return value from connect ?
qDebug() << "i work:" << connect(&worker, SIGNAL(error()), this, SLOT(clError()));
also dont clError take parameters ?
so it should be
connect(&worker, SIGNAL(error( const std::string )), this, SLOT(clError( const std::string)));
?
you should check out new syntax
http://wiki.qt.io/New_Signal_Slot_Syntaxit catches compile time connect errors.
-
Thanks for the tip.
I have tried with the new connect syntax, but it didn't solve it:
connect(&worker, &Worker::error, this, &MyWidget::clError);
connect(this, &MyWidget::start, &worker, &Worker::run);I'll try qDebug() soon.
By the way do you know how to enable qDebug in a cmake project? -
@Andy32
Hi
ok, did you tried with Qt:QueuedConnection
(last param in connect)
You seems to be sending across threads as QWidget must live in main thread but i didnt study code in details.
It should be automatic but its worth testing. -
Hi,
With Qt::QueuedConnection it is still not working.
std::cout << "error connect: "
<< connect(&worker, &Worker::error, this, &MyWidget::clError, Qt::QueuedConnection) << "\n" << std::flush;
std::cout << "start connect: "
<< connect(this, &MyWidget::start, &worker, &Worker::run, Qt::QueuedConnection) << "\n" << std::flush; -
Hi
Ok. odd. You seems to not overridden qthread run so msg loop should be running.
It didn't complain about std::string as parameter ?
(i assume connect says true)Sorry i cant guess what could be wrong. looks ok from quick look.
seems to be close to
https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ -
Create the worker object in the heap and use the Qt 5 connect syntax so you get compile-time checking. In particular this line:
connect(&worker, SIGNAL(error()), this, SLOT(clError()));
is wrong. The method signatures do not match what you've given to
QObject::connect
. -
@mrjj said in Problem with interthread signals:
It didn't complain about std::string as parameter ?
(i assume connect says true)I have changed std::string into QString and now it is working. connect() returns 1 in both cases. Is there any description about what is good or not good as signal/slot signatures? E.g. can I uses enums?
-
@Andy32 said in Problem with interthread signals:
Is there any description about what is good or not good as signal/slot signatures?