Is QTimer is the only option ?
Hi,
I want to poll continuously some hardware functions like ..
Code:
main(int argc, char *argv[])
{
Widget w; // Widget Class
while (1)
{
//functions to poll continuously
}
w.show();
a.exec();
}
If i execute above code it doesn't show widget on the display, I understand that a.exec() should execute to enter in to the event loop.
But if i write like this...
Code:
main(int argc, char *argv[])
{
Widget w; // Widget Class
w.show();
while (1)
{
//functions to poll continuously
a.exec();
}
}
in while loop only execute once, and then a.exec() wait for the event.
How can I run while loop continuously to poll some function.
Is it mean that I need to start QTimer and need timer event every time?
Or is there any other way that can use while loop?
With Regards,
Nirav
Re: Is QTimer is the only option ?
no u need a seperate event loop along with the main gui event loop ... try QThread() ... it creates a new event loop and communicate with sig/slot ..
Re: Is QTimer is the only option ?
Maybe you can use QSocketNotifier?
Re: Is QTimer is the only option ?
Perhaps you could put your while loop inside the widget class and not on the main function
Re: Is QTimer is the only option ?
Quote:
Originally Posted by
nrabara
[...]
If i execute above code it doesn't show widget on the display, I understand that a.exec() should execute to enter in to the event loop.
That is because you never reach the call to a.exec(). This code continuously executes the loop, and as it can never leave the loop because the loop's condition is always true, the code beyond is never executed.
Quote:
Originally Posted by
nrabara
[...]
in while loop only execute once, and then a.exec() wait for the event.
How can I run while loop continuously to poll some function.
Is it mean that I need to start QTimer and need timer event every time?
Here a.exec() starts a new loop, that is why your original loop is only executed once.
If you want to display some data polled from a device you should probably use a seperate thread that does the polling and notifies the GUI via a signal if something changed. Have a look at the thread examples to see how this is done.
Re: Is QTimer is the only option ?
Here's a link to quite interesting wysota's article in QQ, which may be useful here:
http://doc.trolltech.com/qq/qq27-responsive-guis.html
Re: Is QTimer is the only option ?
right mr.calhal ... its a very good article to learn the event loop in qt ...