Results 1 to 6 of 6

Thread: killing Qt timer.

  1. #1
    Join Date
    May 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default killing Qt timer.

    Hi, i have a Qt timer that emits timeout() every 500ms (interval set to 500ms, setSingleShot = true)

    so in my slot for the timeout()

    i do some processing and call timer->start(); so that it will continue to call timeout.

    the problem comes when i close the program using the X close button at the top right of the window. I realised that my Qt program is still running in the background.. most prob the timer did not get killed cleanly.

    how do i solve problems like this?

    thanks man.!

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: killing Qt timer.

    Unless you call your application's close() slot, closing the window will simply close the window - it won't shut down the application. At some point, you have to explicitly call close() if you want the application to terminate.

  3. #3
    Join Date
    May 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: killing Qt timer.

    Quote Originally Posted by SixDegrees View Post
    Unless you call your application's close() slot, closing the window will simply close the window - it won't shut down the application. At some point, you have to explicitly call close() if you want the application to terminate.
    hi, sorry i am pretty new to Qt so where do you think is the best place to place the close()?

    Will pressing on the x button emit some sort of signal?

    thanks

  4. #4
    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: killing Qt timer.

    How you create the timer may have some bearing on this problem. Muisei has already suggested one way to make sure the timer object is deleted. Typical setup for the main window in Qt will exit when the user clicks the "X' control, presses Alt-F4 (or equiv), or the code explicitly calls close() on the last remaining top-level window etc. If the timer's parent is the window then it goes too.

    Can you post a small example that behaves this way?

  5. #5
    Join Date
    May 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: killing Qt timer.

    Quote Originally Posted by ChrisW67 View Post
    How you create the timer may have some bearing on this problem. Muisei has already suggested one way to make sure the timer object is deleted. Typical setup for the main window in Qt will exit when the user clicks the "X' control, presses Alt-F4 (or equiv), or the code explicitly calls close() on the last remaining top-level window etc. If the timer's parent is the window then it goes too.

    Can you post a small example that behaves this way?
    the codes are very long so i will just post how it works.

    I have a class that contains a member timer

    When my mainWindow loads, it will create a new instance of that class with the timer and starts it.

    that's about it.

  6. #6
    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: killing Qt timer.

    OK so the timer does not go out of scope until the containing object (let's call it T) does. If the main window (M) creates the T object on the stack, i.e. T as a member variable of M, then T is destroyed when M is. If M allocated T on the heap, i.e. by new statement, then M must delete T directly (in M::~M) or make sure something else does: make T an Q_OBJECT (if it isn't already) and allocate T with M as parent.

    The point of asking for a small example displaying the behaviour is to distil the problem to its bare essentials and remove any surrounding confusing influences. Often the process of doing this is enough to make the actual problem obvious. The code below shows both heap and stack approaches.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. class M1: public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. M1() {
    10. count = 0;
    11. setWindowTitle("Timer on stack");
    12. l = new QLabel("", this);
    13. setCentralWidget(l);
    14. connect(&t, SIGNAL(timeout()), this, SLOT(onTimeout()));
    15. t.start(500);
    16. };
    17.  
    18. public slots:
    19. void onTimeout() { l->setText(QString::number(++count, 10)); };
    20.  
    21. private:
    22. QTimer t;
    23. QLabel *l;
    24. int count;
    25. };
    26.  
    27.  
    28. class M2: public QMainWindow
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. M2() {
    34. count = 0;
    35. setWindowTitle("Timer on heap");
    36. l = new QLabel("", this);
    37. setCentralWidget(l)
    38. t = new QTimer(this); // Qt will look after delete-ing the timer
    39. connect(t, SIGNAL(timeout()), this, SLOT(onTimeout()));
    40. t->start(500);
    41. };
    42.  
    43. public slots:
    44. void onTimeout() { l->setText(QString::number(++count, 10)); };
    45.  
    46. private:
    47. QTimer *t;
    48. QLabel *l;
    49. int count;
    50. };
    51.  
    52. int main(int argc, char **argv)
    53. {
    54. QApplication a(argc, argv);
    55.  
    56. M1 m1;
    57. M2 m2;
    58. m1.show();
    59. m2.show();
    60.  
    61. return a.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Accuracy of the timer in Qt
    By Fastman in forum Qt Programming
    Replies: 5
    Last Post: 21st July 2009, 21:52
  2. Timer delay
    By bmn in forum Qt Programming
    Replies: 11
    Last Post: 3rd June 2008, 11:54
  3. Killing a Window in its constructor
    By hardgeus in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2006, 18:31
  4. Timer call
    By mahe2310 in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2006, 08:57
  5. Timer not timing out?
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 2nd March 2006, 21:30

Tags for this Thread

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.