Solved QWebChannel signal Qt::ConnectionType
-
Hello there
To explain my problem i have to explain my situation first. My Project contains a Server and a Client. My code on the server looks like this :
void LongFunction(){ emit Log("Start doing something that might take some time"); //do something that takes much time std::this_thread::sleep_for(20s); emit Log("Done doing something that might take some time"); }
When i call the "LongFunction" from my client i want to show the log messages in the Client GUI. Therefore i connect to the "Log" signal. But here comes my problem the signals will be emitted / triggered after "LongFunction" returns. Which makes my logs not so usefull.
While doing some tests on the Server only i experienced that my problems does not occur on the Server if i use the Qt::ConnectionType "Qt::DirectConnection".My question now is: Is it possible to set the Qt::ConnectionType from the QWebChannel JS Client? If thats not possible is there some kind of other workaround that allows me to emit / trigger my signals immediately instead of when the function returns.
Kind Rregards
Gino Pio Loco -
@gpioloco To explain your problem: Qt::DirectConnection means that the slot connected to the signal will be executed as if you would call the slot directly. This is why it works in this case.
If you do not use Qt::DirectConnection and emit a signal then the signal is put into a queue and will be fetched from the queue next time event loop runs. But, since you do long lasting stuff after emitting signal the event loop is blocked.
What you could do to let the event loop do its work after emitting the signal is calling https://doc.qt.io/qt-5/qcoreapplication.html#processEvents after first emit.
Alternative would be to use a one shot timer with 0 as time out value and move the long lasting stuff into a slot connected to the time out signal of the timer. -
I already tried to call "QCoreApplication::processEvents();" after the emit but that did not help. I will try the timer workaround and write back to you if it worked.
Thanks for the quick response -
Update:
I figured out a workaround using the a timer. Thanks for the help.