PDA

View Full Version : Is QTimer is the only option ?



nrabara
4th May 2009, 06:16
Hi,

I want to poll continuously some hardware functions like ..


main(int argc, char *argv[])
{
Widget w; // Widget Class
QApplication a(argc, argv);
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...


main(int argc, char *argv[])
{
Widget w; // Widget Class
QApplication a(argc, argv);

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

wagmare
4th May 2009, 06:28
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 ..

wysota
4th May 2009, 12:24
Maybe you can use QSocketNotifier?

john_god
4th May 2009, 18:18
Perhaps you could put your while loop inside the widget class and not on the main function

rexi
4th May 2009, 18:36
[...]

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.


[...]

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.

calhal
5th May 2009, 16:35
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

wagmare
6th May 2009, 06:59
right mr.calhal ... its a very good article to learn the event loop in qt ...