4412開發(fā)板Qt定時(shí)器-實(shí)驗(yàn)步驟和部分代碼
發(fā)布時(shí)間:2021-5-15 10:44
發(fā)布者:
落風(fēng)
實(shí)驗(yàn)?zāi)繕?biāo):實(shí)現(xiàn)計(jì)時(shí)器功能,并且點(diǎn)擊打點(diǎn)按鈕將當(dāng)前時(shí)間打印出來(lái)。
用到的類有 QTimer 和 QTime,QTimer 是一個(gè)計(jì)時(shí)器類,相當(dāng)于秒表,QTimer 是一個(gè)時(shí)間類,相當(dāng)于手表。
一:實(shí)驗(yàn)步驟(迅為4412開發(fā)板)
步驟一:界面布局:
拖拽組件,在屬性編輯欄設(shè)置大小,然后選中按鈕,點(diǎn)擊水平布局;
在屬性編輯欄設(shè)置 Label 的最小高度為 50,選中全部組件,點(diǎn)擊柵格布局,如圖:
根據(jù)實(shí)際情況調(diào)整大小,更改對(duì)象名后如下圖:
步驟二:創(chuàng)建計(jì)時(shí)器類對(duì)象 timer 和時(shí)間類 time,設(shè)置初始時(shí)間為 0。
- class TimerP : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit TimerP(QWidget *parent = 0); ~TimerP();
- QTimer timer;
- QTime time;
- .......... };
復(fù)制代碼
步驟三:開啟計(jì)時(shí)器對(duì)象,設(shè)置定時(shí)時(shí)間,時(shí)間到后會(huì)發(fā)出 timeout() 信號(hào),綁定此信號(hào)和自定義的槽函數(shù) timeOut_Slot()。
void start(int msec);
函數(shù)功能:開啟定時(shí)器,時(shí)間到后發(fā)出 timeout 信號(hào),并重新計(jì)時(shí)。
參數(shù) msec 含義:定時(shí)時(shí)間,單位毫秒。
- TimerP::TimerP(QWidget *parent) :
- QMainWindow(parent), ui(new Ui::TimerP)
- {
- ui->setupUi(this);
- //信號(hào) timeout 與槽函數(shù)綁定
- connect(&timer,SIGNAL(timeout()),this,SLOT(timeOut_Slot()));
- time.setHMS(0,0,0,0);
- ui->showTime->setText("00:00:00:000");
- }
- /**開始定時(shí)
- */
- void TimerP::on_starBu_clicked()
- {
- timer.start(3);
- }
復(fù)制代碼
步驟四:槽函數(shù) timeOut_Slot()內(nèi)處理時(shí)間類對(duì)象,使每次計(jì)時(shí)時(shí)間結(jié)束后,時(shí)間對(duì)象能增加相同的時(shí)間,實(shí)現(xiàn)計(jì)時(shí)功能。
QTime addMSecs(int ms) const;
參數(shù) msec 含義:增加的時(shí)間值,單位毫秒。
函數(shù)功能:返回一個(gè)當(dāng)前時(shí)間對(duì)象之后 ms 毫秒之后的時(shí)間對(duì)象。
- /*
- * 計(jì)時(shí)
- */
- void TimerP::timeOut_Slot()
- {
- //qDebug("timt out");
- time = time.addMSecs(3);
- ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
- }
復(fù)制代碼
步驟五:打點(diǎn)記錄功能,使用全局變量記錄排名,并顯示到界面。
- /*
- * 記錄
- */
- 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"));
- }
復(fù)制代碼
二:部分代碼
- 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();//開始計(jì)時(shí)按鈕槽函數(shù)
- void timeOut_Slot();//定時(shí)時(shí)間到槽函數(shù)
- void on_closeBu_clicked();//關(guān)閉按鈕槽函數(shù)
- void on_resetBu_clicked();//重置按鈕槽函數(shù)
- void on_bitBu_clicked();//打點(diǎn)記錄按鈕槽函數(shù)
- 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);
- }
- /*
- * 處理時(shí)間類對(duì)象
- */
- void TimerP::timeOut_Slot()
- {
- //qDebug("timt out");
- time = time.addMSecs(3);
- ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
- }
- /*
- * 關(guān)閉
- */
- 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"));
- }
復(fù)制代碼
|