關(guān)鍵詞網(wǎng)站優(yōu)化平臺(tái)營(yíng)銷型網(wǎng)站有哪些平臺(tái)
????????Qt中提供了兩種定時(shí)器的方式一種是使用Qt中的事件處理函數(shù),另一種就是Qt中的定時(shí)器類QTimer。
? ? ? ? 使用QTimer類,需要?jiǎng)?chuàng)建一個(gè)QTimer類對(duì)象,然后調(diào)用其start()方法開啟定時(shí)器,此后QTimer對(duì)象就會(huì)周期性的發(fā)出timeout()信號(hào)。
1.QTimer類中一些相關(guān)的API
1.1 pulic/slot function
// 構(gòu)造函數(shù)
// 如果指定了父對(duì)象, 創(chuàng)建的堆內(nèi)存可以自動(dòng)析構(gòu)
QTimer::QTimer(QObject *parent = nullptr);// 設(shè)置定時(shí)器時(shí)間間隔為 msec 毫秒
// 默認(rèn)值是0,一旦窗口系統(tǒng)事件隊(duì)列中的所有事件都已經(jīng)被處理完,一個(gè)時(shí)間間隔為0的QTimer就會(huì)觸發(fā)
void QTimer::setInterval(int msec);
// 獲取定時(shí)器的時(shí)間間隔, 返回值單位: 毫秒
int QTimer::interval() const;// 根據(jù)指定的時(shí)間間隔啟動(dòng)或者重啟定時(shí)器, 需要調(diào)用 setInterval() 設(shè)置時(shí)間間隔
[slot] void QTimer::start();
// 啟動(dòng)或重新啟動(dòng)定時(shí)器,超時(shí)間隔為msec毫秒。
[slot] void QTimer::start(int msec);
// 停止定時(shí)器。
[slot] void QTimer::stop();// 設(shè)置定時(shí)器精度
/*
參數(shù): - Qt::PreciseTimer -> 精確的精度, 毫秒級(jí)- Qt::CoarseTimer -> 粗糙的精度, 和1毫秒的誤差在5%的范圍內(nèi), 默認(rèn)精度- Qt::VeryCoarseTimer -> 非常粗糙的精度, 精度在1秒左右
*/
void QTimer::setTimerType(Qt::TimerType atype);
Qt::TimerType QTimer::timerType() const; // 獲取當(dāng)前定時(shí)器的精度// 如果定時(shí)器正在運(yùn)行,返回true; 否則返回false。
bool QTimer::isActive() const;// 判斷定時(shí)器是否只觸發(fā)一次
bool QTimer::isSingleShot() const;
// 設(shè)置定時(shí)器是否只觸發(fā)一次, 參數(shù)為true定時(shí)器只觸發(fā)一次, 為false定時(shí)器重復(fù)觸發(fā), 默認(rèn)為false
void QTimer::setSingleShot(bool singleShot);
?1.2 signals
????????QTimer這個(gè)類的信號(hào)只有一個(gè), 當(dāng)定時(shí)器超時(shí)時(shí),該信號(hào)就會(huì)被發(fā)射出來。給這個(gè)信號(hào)通過conect()關(guān)聯(lián)一個(gè)槽函數(shù), 就可以在槽函數(shù)中處理超時(shí)事件了
?
//當(dāng)定時(shí)器超時(shí)時(shí),該信號(hào)就會(huì)發(fā)出
[signal] void QTimer::timeout();
1.3QTimer類中的靜態(tài)公共函數(shù)(static public function)
// 其他同名重載函數(shù)可以自己查閱幫助文檔
/*
功能: 在msec毫秒后發(fā)射一次信號(hào), 并且只發(fā)射一次
參數(shù):- msec: 在msec毫秒后發(fā)射信號(hào)- receiver: 接收信號(hào)的對(duì)象地址- method: 槽函數(shù)地址
*/
[static] void QTimer::singleShot(int msec, const QObject *receiver, PointerToMemberFunction method);
2.QTimer代碼例子
創(chuàng)建一個(gè)新工程,在mianwindow.ui文件中放入一個(gè)PushButton,并修改按鈕名稱和對(duì)象名稱:
?
再添加一個(gè)Label并改變對(duì)象名稱,這個(gè)Label用來顯示當(dāng)前的時(shí)間?
在拷貝一份PushButton和Label,修改Pushbutton和Label對(duì)象名稱
?
//mianwindow.cpp#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QTime>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//創(chuàng)建定時(shí)器對(duì)象QTimer *timer = new QTimer(this);//按鈕的點(diǎn)擊事件connect(ui->loopBtn,&QPushButton::clicked, this, [=]{//啟動(dòng)定時(shí)器if(timer->isActive()){timer->stop(); //關(guān)閉定時(shí)器ui->loopBtn->setText("開始");}else{ui->loopBtn->setText("關(guān)閉");timer->start(1000); //開啟定時(shí)器 1秒鐘觸發(fā)定時(shí)器信號(hào) 1000ms = 1s}});connect(timer, &QTimer::timeout, this,[=]{QTime tm = QTime::currentTime();//獲取當(dāng)前時(shí)間//格式化當(dāng)前得到的系統(tǒng)時(shí)間QString tmstr = tm.toString("hh:mm:ss.zzz");//設(shè)置要顯示的時(shí)間ui->curtime->setText(tmstr);});//發(fā)射一次信號(hào)connect(ui->onceBtn, &QPushButton::clicked,this,[=]{//獲取2s以后的系統(tǒng)時(shí)間QTimer::singleShot(2000,this,[=]{QTime tm = QTime::currentTime();//獲取當(dāng)前時(shí)間//格式化當(dāng)前得到的系統(tǒng)時(shí)間QString tmstr = tm.toString("hh:mm:ss.zzz");//設(shè)置要顯示的時(shí)間ui->oncetime->setText(tmstr);});});
}MainWindow::~MainWindow()
{delete ui;
}
運(yùn)行結(jié)果:
?第一個(gè)開始按鈕按下,時(shí)間定時(shí)開始計(jì)時(shí),點(diǎn)擊關(guān)閉停止計(jì)時(shí);
第二個(gè)開始按鈕按下,會(huì)等待2秒后才顯示當(dāng)前系統(tǒng)時(shí)間。