QT-Qt組件QTimer使用方法


相關資料:

https://blog.csdn.net/u014783974/article/details/81486491

 

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 #include <QTimer>
 7 
 8 QT_BEGIN_NAMESPACE
 9 namespace Ui { class MainWindow; }
10 QT_END_NAMESPACE
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     MainWindow(QWidget *parent = nullptr);
18     ~MainWindow();
19 
20 private slots:
21     void on_pushButton_clicked();
22     void TimerTimeOut();
23     void on_pushButton_2_clicked();
24     void on_pushButton_3_clicked();
25 
26 private:
27     Ui::MainWindow *ui;
28     QTimer *m_timer;
29     void InitTimer();
30 };
31 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     setWindowTitle(QStringLiteral("Qt組件QTimer使用方法"));
10     InitTimer();
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 
19 void MainWindow::on_pushButton_clicked()
20 {
21     ui->textEdit->clear();
22 }
23 
24 void MainWindow::TimerTimeOut()
25 {
26 //    //判斷定時器是否運行
27 //    if(m_timer->isActive())
28 //        m_timer->stop();   //停止定時器
29     ui->textEdit->append("------");
30 
31 }
32 
33 void MainWindow::InitTimer()
34 {
35     if(NULL == m_timer)
36        m_timer = new QTimer;
37    //設置定時器是否為單次觸發。默認為 false 多次觸發
38    m_timer->setSingleShot(false);
39    //啟動或重啟定時器, 並設置定時器時間:毫秒
40    m_timer->start(1000);
41    //定時器觸發信號槽
42    connect(m_timer, &QTimer::timeout, this, &MainWindow::TimerTimeOut);
43 
44 }
45 
46 void MainWindow::on_pushButton_2_clicked()
47 {
48     m_timer->start();
49 }
50 
51 void MainWindow::on_pushButton_3_clicked()
52 {
53 
54     m_timer->stop();
55 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM