實驗目標:實現計時器功能,并且點擊打點按鈕將當前時間打印出來。
用到的類有 QTimer 和 QTime,QTimer 是一個計時器類,相當于秒表,QTimer 是一個時間類,相當于手表。
一:實驗步驟(迅為4412開發板)
步驟一:界面布局:
拖拽組件,在屬性編輯欄設置大小,然后選中按鈕,點擊水平布局;
在屬性編輯欄設置 Label 的最小高度為 50,選中全部組件,點擊柵格布局,如圖:
根據實際情況調整大小,更改對象名后如下圖:
步驟二:創建計時器類對象 timer 和時間類 time,設置初始時間為 0。
1 2 3 4 5 6 7 8 | class TimerP : public QMainWindow { Q_OBJECT public: explicit TimerP(QWidget *parent = 0); ~TimerP(); QTimer timer; QTime time; .......... }; |
步驟三:開啟計時器對象,設置定時時間,時間到后會發出 timeout() 信號,綁定此信號和自定義的槽函數 timeOut_Slot()。
void start(int msec);
函數功能:開啟定時器,時間到后發出 timeout 信號,并重新計時。
參數 msec 含義:定時時間,單位毫秒。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | TimerP::TimerP(QWidget *parent) : QMainWindow(parent), ui(new Ui::TimerP) { ui->setupUi(this); //信號 timeout 與槽函數綁定 connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot())); time.setHMS(0,0,0,0); ui->showTime->setText('00:00:00:000'); } /**開始定時 */ void TimerP::on_starBu_clicked() { timer.start(3); } |
步驟四:槽函數 timeOut_Slot()內處理時間類對象,使每次計時時間結束后,時間對象能增加相同的時間,實現計時功能。
QTime addMSecs(int ms) const;
參數 msec 含義:增加的時間值,單位毫秒。
函數功能:返回一個當前時間對象之后 ms 毫秒之后的時間對象。
1 2 3 4 5 6 7 8 9 | /* * 計時 */ void TimerP::timeOut_Slot() { //qDebug('timt out'); time = time.addMSecs(3); ui->showTime->setText(time.toString('hh:mm:ss.zzz')); } |
步驟五:打點記錄功能,使用全局變量記錄排名,并顯示到界面。
1 2 3 4 5 6 7 8 9 10 11 | /* * 記錄 */ void TimerP::on_bitBu_clicked() { QString temp; i=i+1; temp.sprintf('%d: ',i); ui->bitTime->append(temp); ui->bitTime->append(time.toString('hh:mm:ss.zzz')); } |
二:部分代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | timerp.h: class TimerP : public QMainWindow { Q_OBJECT public: explicit TimerP(QWidget *parent = 0); ~TimerP(); QTimer timer; QTime time; private slots: void on_starBu_clicked();//開始計時按鈕槽函數 void timeOut_Slot();//定時時間到槽函數 void on_closeBu_clicked();//關閉按鈕槽函數 void on_resetBu_clicked();//重置按鈕槽函數 void on_bitBu_clicked();//打點記錄按鈕槽函數 private: Ui::TimerP *ui; }; timerp.cpp: #include #include static int i; TimerP::TimerP(QWidget *parent) : QMainWindow(parent), ui(new Ui::TimerP) { ui->setupUi(this); connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot())); time.setHMS(0,0,0,0); ui->showTime->setText('00:00:00:000'); } TimerP::~TimerP() { delete ui; } void TimerP::on_starBu_clicked() { timer.start(3); } /* * 處理時間類對象 */ void TimerP::timeOut_Slot() { //qDebug('timt out'); time = time.addMSecs(3); ui->showTime->setText(time.toString('hh:mm:ss.zzz')); } /* * 關閉 */ void TimerP::on_closeBu_clicked() { timer.stop(); i=0; } /* * 重置 */ void TimerP::on_resetBu_clicked() { timer.stop(); time.setHMS(0,0,0,0); ui->showTime->setText('00:00:00:000'); ui->bitTime->clear(); i=0; } /* * 記錄 */ void TimerP::on_bitBu_clicked() { QString temp; i=i+1; temp.sprintf('%d: ',i); ui->bitTime->append(temp); ui->bitTime->append(time.toString('hh:mm:ss.zzz')); } |
運行結果:
上一篇:迅為4412開發板Qt網絡編程-UDP實現服務器和客戶端
下一篇:迅為-iMX6ULL開發板--C程序調用shell
推薦閱讀最新更新時間:2025-04-11 10:11


