Results 1 to 6 of 6

Thread: When is timeout signal fired (Qtimer)

  1. #1
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default When is timeout signal fired (Qtimer)

    Is timeout() signal fired when I explicitly stop the timer? If yes, what should I do to prevent it from being called?
    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: When is timeout signal fired (Qtimer)

    No, the timeout() signal is issued when the timer has expired and the program has returned to the Qt event loop. Calling stop() immediately kills the timer.

  3. #3
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When is timeout signal fired (Qtimer)

    Here is my code. I want that the msg box appears ONCE if the cursor stays inside the widget for two sec continuously.But when i move the cursor in (for 1sec) and then out, and again in for two sec, the message box appears for Random no of time..
    what am i doing wrong??
    Custom1.h

    Qt Code:
    1. #ifndef CUSTOM1_H
    2. #define CUSTOM1_H
    3. #include<QPushButton>
    4. #include<QTimer>
    5.  
    6. class Custom1 : public QPushButton
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit Custom1(QWidget* parent = 0) : QPushButton(parent)
    12. {
    13. connect(this, SIGNAL(clicked()), this, SLOT(display_in()));
    14. connect(this, SIGNAL(pressed()), this, SLOT(display_out()));
    15. }
    16. ~Custom1() {}
    17.  
    18. protected:
    19. void enterEvent(QEvent*);
    20. void leaveEvent(QEvent*);
    21.  
    22. public slots:
    23. void display_in();
    24. void display_out();
    25. void display();
    26.  
    27. };
    28.  
    29.  
    30.  
    31. #endif // CUSTOM1_H
    To copy to clipboard, switch view to plain text mode 

    Custom1.cpp

    Qt Code:
    1. #include "custom1.h"
    2. #include<QTimer>
    3. #include<QMessageBox>
    4.  
    5. QTimer *my_timer = new QTimer();
    6.  
    7. void Custom1::enterEvent(QEvent*)
    8. {
    9. Q_EMIT clicked();
    10. connect(my_timer, SIGNAL(timeout()), this, SLOT(display()));
    11.  
    12. my_timer->setSingleShot(true);
    13. my_timer->start(2000);
    14.  
    15. }
    16.  
    17. void Custom1::leaveEvent(QEvent*)
    18. {
    19.  
    20. if (my_timer->isActive())
    21. {
    22. my_timer->stop();
    23. }
    24.  
    25.  
    26. Q_EMIT pressed();
    27. }
    28.  
    29. void Custom1::display_in()
    30. {
    31. setText("CURSOR IN");
    32. }
    33.  
    34. void Custom1::display_out()
    35. {
    36. setText("CURSOR OUT");
    37. }
    38.  
    39. void Custom1::display()
    40. {
    41.  
    42. a++;
    43. setText(QString::number(a));
    44.  
    45.  
    46.  
    47.  
    48. msg.setText("hi"); // <--- THIS MESSAGE BOX APPEARS SOME RANDOM NO OF TIMES... WHY??
    49. msg.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 

  4. #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: When is timeout signal fired (Qtimer)

    Please provide a minimal compilable example reproducing the problem.
    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.


  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When is timeout signal fired (Qtimer)

    First of all timer signal is emmited AFTER 2 sec. If in this 2 sec Custom1::enterEvent(QEvent*) is called a few times timer signal is connected to display many times. Secondly, you never disconnect timer signal from display slot.

  6. The following 2 users say thank you to Lesiok for this useful post:

    aamir_azad (9th February 2013), learner_qt (9th February 2013)

  7. #6
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When is timeout signal fired (Qtimer)

    Thanks Lesiok!!!

    got the mistake!!!

    Now its working

Similar Threads

  1. Replies: 15
    Last Post: 4th August 2012, 19:11
  2. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  3. Get QTimer's seconds till next timeout?
    By hakermania in forum Newbie
    Replies: 12
    Last Post: 12th February 2011, 23:49
  4. QTimer delayed timeout
    By DavidY in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 21:34
  5. Replies: 2
    Last Post: 9th September 2009, 00:26

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.