#include #include #include #include #include #include class MainWindow : public QWidget { Q_OBJECT int cnt = 0; QTimer *tmr = 0; public: explicit MainWindow (QWidget *parent = 0): QWidget (parent) { QHBoxLayout *layout = new QHBoxLayout (this); QPushButton *pb = new QPushButton (this); layout->addWidget (pb); connect (pb, SIGNAL(clicked()), this, SLOT(initTimer())); } public slots: void initTimer (void) { cnt=5; if (!tmr) { tmr = new QTimer(this); connect (tmr, SIGNAL(timeout()), this, SLOT(updateCnt())); } tmr->start (1000); } void updateCnt (void) { qDebug () << __func__ << ": cnt is " << cnt; cnt--; if (cnt == 0) { qDebug () << __func__ << ": deleting timer"; tmr->stop (); delete tmr; } } }; int main (int argc, char *argv[]) { QApplication a(argc, argv); MainWindow mv; mv.show (); return a.exec(); } #include "main.moc"