Advertisement
Ejejejejejjr

Таймер(QT)

Jan 2nd, 2021
1,957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // заголовочный
  2. #pragma once
  3.  
  4. #include <QWidget>
  5. #include <QLabel>
  6.  
  7. class Events : public QWidget {
  8.  
  9. public:
  10.     Events(QWidget *parent = 0);
  11.  
  12. protected:
  13.     void timerEvent(QTimerEvent *e);
  14.  
  15. private:
  16.     QLabel *label;
  17.  
  18. };
  19. //файл реализации
  20. #include <QHBoxLayout>
  21. #include <QTime>
  22.  
  23. #include "events.h"
  24.  
  25. Events::Events(QWidget *parent)
  26.     : QWidget(parent)
  27. {
  28.  
  29.     //создание компоновки
  30.     QHBoxLayout *hbox = new QHBoxLayout(this);
  31.     //установка пробела между элементами
  32.     hbox->setSpacing(5);
  33.  
  34.     label = new QLabel("", this);
  35.     //добавление в виджет
  36.     hbox->addWidget(label, 0, Qt::AlignLeft | Qt::AlignTop);
  37.  
  38.     QTime qtime = QTime::currentTime();
  39.     QString stime = qtime.toString();
  40.     //установка текста виджета
  41.     label->setText(stime);
  42.  
  43.     //односекундный таймер
  44.     startTimer(1000);
  45.  
  46. }
  47.  
  48.  
  49. void Events::timerEvent(QTimerEvent *e) {
  50.  
  51.   Q_UNUSED(e);
  52.  
  53.   QTime qtime = QTime::currentTime();
  54.   QString stime = qtime.toString();
  55.   label->setText(stime);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement