PDA

View Full Version : QBasicTimer never timeout.



kunalnandi
29th January 2010, 11:15
Hi,

I am facing problem with QBasicTimer, here i am posting my demo code which will reproduce and explain my problem.



#include <QCoreApplication>
#include <QBasicTimer>
#include <iostream>

using namespace std;

class MyClass:public QObject
{
public:
MyClass(){};
};

int main( int argc, char *argv[] )
{
QCoreApplication app( argc, argv );
MyClass *mc = new MyClass();
QBasicTimer bt;
bt.start( 120 * 1000, mc );

while( bt.isActive() == true ) //It should get timeout after 2 minute, but here its not the case.
{
cout << "Basic Timer is Running : " << bt.timerId() << " ..!!!!!" << endl;
}

return app.exec();
}


Output...

Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
Basic Timer is Running : 1 ..!!!!
.
.
.
.
.
//infinite time !!


When the timer will get timeout ? or I have to stop it explicitly to get Timer state isActive() = false ???

high_flyer
29th January 2010, 11:32
Did you read the QBasicTimer documentation?

You didn't connect the the timer to a slot,so you can't know when it sends a timeout.

I also think you are misunderstanding the meaning of timeout here.
The timer will timeout again and again after the given interval.
And as long as it is not stopped it is active, even between timeouts, since it has to count the time.

If you want to use the timer only once, use QTimer::singleShot().

Is there a special reason you are using QBasicTimer and not QTimer?

In addition, you are checking your timer before app.exec(), so the application event loop is not running, so even if the timer would be connected to a slot it would not fire, since the event would not get delivered to the widget.

And please read the docs!

faldzip
29th January 2010, 11:36
Did you read QBasicTimer documentation? I guess not.

Timers (QBasicTimer and QTimer) when started they are running all the time but sending event on timeout. It is preferred to use QTimer then QBasicTimer. QTimer emits signal timeout() on timeout (surprise! :P) and QBasicTimer sends timer event to the given QObject subclass, but I don't see any timer event handling in MyClass...

Please read documentation about QBasicTimer and QTimer to get how to use them.

EDIT:
Oops... I was a bit late. But it is good that I'm not alone with opinion about thread's creator's documentation knowledge :P

wysota
29th January 2010, 20:21
The main problem is that the event loop is not running :) Use of QBasicTimer instead of QTimer is a secondary issue.

kunalnandi
30th January 2010, 06:37
Thanks for your replies and I appreciate that you all have given some time to reply for this stupid kind of issue.

@wysota : do we really need event loop for QBasicTimer, because its no where stated in the doc that QBasicTimer needs event loop like QTimer.

@faldżip : yes, i accept that's my mistake there is nothing in MyCalss which will handle the timer event which QBasicTimer will send to MyClass.

@high_flyer : do we really need Event loop for QBasicTimer ?? and yes there is a special need where i have to use QBasicTimer instead of QTimer.

faldzip
30th January 2010, 07:55
do we really need Event loop for QBasicTimer ??
QBasicTimer is sending timer event on timeout, so as you can guess event loop is required, because it is event-based.

and yes there is a special need where i have to use QBasicTimer instead of QTimer.
What is this special need?