Results 1 to 6 of 6

Thread: QBasicTimer never timeout.

  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question QBasicTimer never timeout.

    Hi,

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

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QBasicTimer>
    3. #include <iostream>
    4.  
    5. using namespace std;
    6.  
    7. class MyClass:public QObject
    8. {
    9. public:
    10. MyClass(){};
    11. };
    12.  
    13. int main( int argc, char *argv[] )
    14. {
    15. QCoreApplication app( argc, argv );
    16. MyClass *mc = new MyClass();
    17. bt.start( 120 * 1000, mc );
    18.  
    19. while( bt.isActive() == true ) //It should get timeout after 2 minute, but here its not the case.
    20. {
    21. cout << "Basic Timer is Running : " << bt.timerId() << " ..!!!!!" << endl;
    22. }
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    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 ???

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QBasicTimer never timeout.

    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 QBasicTimerand 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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QBasicTimer never timeout.

    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
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. The following user says thank you to faldzip for this useful post:

    kunalnandi (30th January 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QBasicTimer never timeout.

    The main problem is that the event loop is not running Use of QBasicTimer instead of QTimer is a secondary issue.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QBasicTimer never timeout.

    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.

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QBasicTimer never timeout.

    Quote Originally Posted by kunalnandi View Post
    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?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. QHttp Timeout?
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 25th February 2010, 09:43
  2. QTimer QBasicTimer and QObject::startTimer
    By SABROG in forum Qt Programming
    Replies: 7
    Last Post: 15th June 2009, 17:37
  3. Tootip Timeout
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2008, 10:17
  4. QTcpSocket timeout problem
    By altVis in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 13:56
  5. QTcpSocket + timeout
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2008, 13:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.