重要函數(shù):
1.int startTimer(int); //設置定時器,返回一個ld.
2.int event->timerld(); //返回當前的ld.
3.void killTimer(int); //停止定時器.
創(chuàng)新互聯(lián)自成立以來,一直致力于為企業(yè)提供從網站策劃、網站設計、網站設計制作、成都做網站、電子商務、網站推廣、網站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯(lián)網的全面整合營銷服務。公司擁有豐富的網站建設和互聯(lián)網應用系統(tǒng)開發(fā)管理經驗、成熟的應用系統(tǒng)解決方案、優(yōu)秀的網站開發(fā)工程師團隊及專業(yè)的網站設計師團隊。
首先從Qt設計師中拖拽出三個按鈕,由于只是演示定時器事件的使用,所以就沒有布局的需要了.
以下是”c.cpp”的代碼:
#include "c.h"c::c(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this); //連接信號與槽.
connect(ui.startButton, SIGNAL(clicked()), this, SLOT(startTimerSlot()));
connect(ui.stopButton, SIGNAL(clicked()), this, SLOT(stopTimerSlot()));
}
c::~c()
{
}void c::timerEvent(QTimerEvent*event)
{ //判斷當前定時器對應的是哪個ld.
if (event->timerId() == this->m_lamp)
{ if (this->m_lampStatus == false)
{ //設置按鈕的圖標.
ui.toolButton->setIcon(QIcon("Icons/lamp.png")); this->m_lampStatus = true;
} else
{
ui.toolButton->setIcon(QIcon("Icons/space.png"));
this->m_lampStatus = false;
}
}
}void c::startTimerSlot()
{ //設置定時器,返回一個timerld.注意單位為毫秒,1000毫秒等于1秒.
this->m_lamp = this->startTimer(1000);
}void c::stopTimerSlot()
{ //停止定時器.
this->killTimer(this->m_lamp);
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546以下是”c.h”的代碼:
#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QLabel>#include <QTimerEvent>class c : public QMainWindow
{
Q_OBJECTpublic: c(QWidget *parent = 0);
~c();protected:
//這是一個虛函數(shù),從QEvent繼承而來. void timerEvent(QTimerEvent*event);private slots: void startTimerSlot(); void stopTimerSlot();private:
Ui::cClass ui; int m_lamp; bool m_lampStatus = false;
};#endif // C_H12345678910111213141516171819202122232425262728293031最后是”main.cpp”的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
c w;
w.show(); return a.exec();
}
分享文章:Qt學習:QTimerEvent定時器事件的處理程序代碼示例
文章路徑:http://www.chinadenli.net/article44/giddhe.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、電子商務、品牌網站制作、網站建設、用戶體驗、網站收錄
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)