spacepaste

  1.  
  2. #include <QApplication>
  3. #include <QWidget>
  4. #include <QPushButton>
  5. #include <QTimer>
  6. #include <QDebug>
  7. #include <QBoxLayout>
  8. class MainWindow : public QWidget {
  9. Q_OBJECT
  10. int cnt = 0;
  11. QTimer *tmr = 0;
  12. public:
  13. explicit MainWindow (QWidget *parent = 0): QWidget (parent) {
  14. QHBoxLayout *layout = new QHBoxLayout (this);
  15. QPushButton *pb = new QPushButton (this);
  16. layout->addWidget (pb);
  17. connect (pb, SIGNAL(clicked()), this, SLOT(initTimer()));
  18. }
  19. public slots:
  20. void initTimer (void) {
  21. cnt=5;
  22. if (!tmr) {
  23. tmr = new QTimer(this);
  24. connect (tmr, SIGNAL(timeout()), this, SLOT(updateCnt()));
  25. }
  26. tmr->start (1000);
  27. }
  28. void updateCnt (void) {
  29. qDebug () << __func__ << ": cnt is " << cnt;
  30. cnt--;
  31. if (cnt == 0) {
  32. qDebug () << __func__ << ": deleting timer";
  33. tmr->stop ();
  34. delete tmr;
  35. }
  36. }
  37. };
  38. int main (int argc, char *argv[]) {
  39. QApplication a(argc, argv);
  40. MainWindow mv;
  41. mv.show ();
  42. return a.exec();
  43. }
  44. #include "main.moc"
  45.