while using cpp Qthread, make a simple emit to QML
Hello people, I am new to Qt but researching for a week,
before saying something I am gonna share my code's snippets first
class MyThread : public QThread
void ledSignal(int value);
** mythread.h finish ***
*** mythread.cpp ***
emit ledSignal("led is high");
*** mythread.cpp finish***
and also in qml side I have simple connection listener which is very simple and well working.
The problem is, how can I emit a function from thread, because above the code is not running because of
"emit ledSignal("led is high");"
I am working on raspberry and need to show the state of led on the GUI
Many thanks for your helps.
Hi and welcome to devnet ?
Are you calling
starton your thread at any time ?
Also be aware that with your implementation the signal will be emitted only once and then the thread will stop.
The only simple thing that I want to do
think very simple,
imagine there is a loop for(int i = 0; i < 10, i++) emit sendNumberToFrontEnd(i)....
in qt side; imagine there is a very very simple GUI is showing changed data.
1, 2, 3... +
in reality I am going to listen UART serial port data, and gonna show it, but this is not the case at this time, Because I am getting crazy and I can not even doing such that simple duty.
Don't use such a tight loop your test, your signal is going to be emitted in one row and at best you are only going to see the last one.
Since you are going to use an UART, I guess you are going to use QSerialPort. If that's the case, you are rather going to use the worker object approach. Therefore I'd suggest adopting that from the start and for a dummy implementation, just use a QTimer and connect it to a slot or lambda that will emit your custom signal.
All right there is already implementations for UART.
The other issue that I should solve is, I am going to turn on a led which is connected to a bread board and a led context. And that led should be in a while loop
such as -> while(1) ... turn on led(led pin number, HIGH) and then sleep(5 second) then turn off the led then sleep(5 second again)
in that case I need a while loop, and I want to show off led status which is "Turned On or Off" on a simple GUI
That case, if I write the loop in Qt's cpp main thread, it locks every thing until the loop finish. But loop is not going to be finished because I want an infinite loop.
So I need to do it in a seperate thread and it works, because seperate thread never locks my application process. But now problem comes again, how to show off the led's status from a thread which is running out of current thread.
No need for an infinite loop for that, just use a QTimer that you set to trigger every 5 seconds. Then you can change the led status as well emit the signal without any risk of blocking the event loop. You could even do it in the main thread if switching the led is fast enough.
Those informations well but I could not get proper answer because it is my bad, I did not mention real thing.
think about there is a very simple GUI again only shows a light speed such as
if light is turning on and turning off in 5 second, I want to show in gui "led speed 5 second" or etc.
but there is a touch button also.
so, if I press the touch button, light's speed will decrease to 2.5 second, and I also need to show off "led speed 2.5 second"
and if I press again it will increase 5 second again.
Now. I should write my own sleep function because if I use current sleep function eg: sleep(5)
program will never understand whether if I press the button or not, because it will sleep for 5 second. So that I need to create a loop which should loop for 5 second and any button event occurs I will understand and also break the loop, if there is no event in 5 second automatically it will break the loop, so on and so forth...
Now, even the custom sleep function is a big job by it self also I need to know if there is touch buttun debouncing as well.. :/
So I think QTimer will not work for me because also my led's speed is not static, it will change after a button is pressed.
What is your brillant suggestion.
By the way I am very happy that there is a person and helping us. Thanks dude.
You don't want to try to call
sleep(5)or similar. You want to use
QTimerjust like @SGaist said. You can change timeout on subsequent calls, or stop timers and restart them with different timeout, if necessary.
But what is the chances to access and send an emit function from a thread to GUI ?
If we can, please support me a simple example with detailed, because I am getting very confused also I should solve that issue