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