重要函數(shù):
1.int startTimer(int); //設(shè)置定時器,返回一個ld.
2.int event->timerld(); //返回當(dāng)前的ld.
3.void killTimer(int); //停止定時器.
創(chuàng)新互聯(lián)自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設(shè)計、網(wǎng)站設(shè)計制作、成都做網(wǎng)站、電子商務(wù)、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營銷服務(wù)。公司擁有豐富的網(wǎng)站建設(shè)和互聯(lián)網(wǎng)應(yīng)用系統(tǒng)開發(fā)管理經(jīng)驗(yàn)、成熟的應(yīng)用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團(tuán)隊(duì)及專業(yè)的網(wǎng)站設(shè)計師團(tuán)隊(duì)。
首先從Qt設(shè)計師中拖拽出三個按鈕,由于只是演示定時器事件的使用,所以就沒有布局的需要了.
以下是”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) { //判斷當(dāng)前定時器對應(yīng)的是哪個ld. if (event->timerId() == this->m_lamp) { if (this->m_lampStatus == false) { //設(shè)置按鈕的圖標(biāo). 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() { //設(shè)置定時器,返回一個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學(xué)習(xí):QTimerEvent定時器事件的處理程序代碼示例
文章路徑:http://chinadenli.net/article44/giddhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、電子商務(wù)、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、用戶體驗(yàn)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)