這篇文章主要介紹“qt事件過濾器如何使用”,在日常操作中,相信很多人在qt事件過濾器如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”qt事件過濾器如何使用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),小程序設(shè)計(jì),十多年建站對(duì)銅雕雕塑等多個(gè)方面,擁有多年的網(wǎng)站推廣經(jīng)驗(yàn)。
在嵌入式qt項(xiàng)目中,有時(shí)并不需求屏幕一直亮著,需要一段時(shí)間不操作時(shí),將屏幕背光關(guān)掉,以達(dá)到節(jié)能的目的;
在qt項(xiàng)目中,可以通過重寫事件過濾器來實(shí)現(xiàn)屏幕操作的檢測,加上定時(shí)器的時(shí)間控制,可以實(shí)現(xiàn)指定時(shí)間內(nèi)沒有屏幕操作,給應(yīng)用程序發(fā)送一個(gè)信號(hào);
下面是我寫的一個(gè)測試代碼:
首先是事件過濾器的重寫代碼:
這里我把這個(gè)類做成單實(shí)例的了,這樣可以在應(yīng)用程序中全局使用,(所有界面的類中都可以連接其超時(shí)信號(hào))
ceventfilter.cpp
#include "ceventfilter.h"#include <QDebug>#include <QEvent> CEventFilter::CEventFilter(QObject *parent) : QObject(parent){ m_timeOutMSec = 30000; m_eventTimer = new QTimer; m_eventTimer->setInterval(m_timeOutMSec); connect(m_eventTimer, SIGNAL(timeout()), this, SLOT(onTimerTimeOut())); m_eventTimer->start(m_timeOutMSec); } CEventFilter::~CEventFilter(){ delete m_eventTimer;} bool CEventFilter::eventFilter(QObject *ob, QEvent *e){ if( e->type()==QEvent::MouseMove||e->type()==QEvent::MouseButtonPress ||e->type()==QEvent::MouseButtonRelease)// 判斷如果是鼠標(biāo)移動(dòng)事件 { if(m_eventTimer->isActive())//have_dosthtimer很顯然是個(gè)定時(shí)器,在這判斷是否已經(jīng)開啟. { m_eventTimer->stop(); m_eventTimer->start();//如果已經(jīng)開啟,并且有鼠標(biāo)移動(dòng)事件就需要計(jì)時(shí)器重新計(jì)算(這里是30s) //qDebug()<<"detect touch event, restart timer, Event type: "<<e->type(); } } return QObject::eventFilter(ob,e);//這里是把事件發(fā)送出去,} //超時(shí)發(fā)送信號(hào)void CEventFilter::onTimerTimeOut(){ qDebug()<<m_timeOutMSec<<" ms not operation ..."; emit noOperationDetect();}//設(shè)置超時(shí)時(shí)間void CEventFilter::setTimeOutSecond(int sec){ m_timeOutMSec = sec*1000; m_eventTimer->stop(); m_eventTimer->setInterval(m_timeOutMSec); m_eventTimer->start(); } CEventFilter *CEventFilter::m_instance = NULL;CEventFilter *CEventFilter::getInstance(){ if ( m_instance == NULL ) { m_instance = new CEventFilter; } return m_instance;}
頭文件:
ceventfilter.h
#ifndef CEVENTFILTER_H#define CEVENTFILTER_H #include <QObject>#include <QTimer> #define TIME_OUT_TIME 30000 class CEventFilter : public QObject{ Q_OBJECTprotected: explicit CEventFilter(QObject *parent = 0); ~CEventFilter(); static CEventFilter *m_instance; virtual bool eventFilter(QObject *ob, QEvent *e); //重寫事件過濾器 public: static CEventFilter *getInstance(); void setTimeOutSecond(int sec); //設(shè)置超時(shí)時(shí)間 signals: void noOperationDetect(); //時(shí)間超時(shí)時(shí)發(fā)送信號(hào) public slots: void onTimerTimeOut(); private: int m_timeOutMSec; QTimer *m_eventTimer;}; #endif // CEVENTFILTER_H
調(diào)用代碼:
//獲取實(shí)例,連接槽函數(shù) m_pEventFilter = CEventFilter::getInstance(); connect(m_pEventFilter, SIGNAL(noOperationDetect()), this, SLOT(onNoOperationDetect()));
這里做了一個(gè)按鈕,是交替斷開與連接事件過濾器超時(shí)信號(hào)的,為了測試信號(hào)的:
void MainWindow::on_pushButton_clicked(){ m_pEventFilter->setTimeOutSecond(3); if(m_connectFlag) { m_connectFlag = false; qDebug()<<"diconnect signal slots"; ui->msg->setText("diconnect signal slots"); m_pEventFilter->disconnect(); } else { m_connectFlag = true; qDebug()<<"connect signal slots"; ui->msg->setText("connect signal slots"); connect(m_pEventFilter, SIGNAL(noOperationDetect()), this, SLOT(onNoOperationDetect())); }}
槽函數(shù):
在界面上拉了一個(gè)label控件,用于顯示提示信息:
void MainWindow::onNoOperationDetect(){ ui->msg->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+": non operate detect"); qDebug()<<"non operate detect";}
這樣在超時(shí)時(shí),會(huì)在label上顯示出來時(shí)間和信息:
這里我設(shè)置的是3s超時(shí),如果鼠標(biāo)不操作界面,3s會(huì)在label上更新顯示信息,如果一直點(diǎn)界面,就不會(huì)超時(shí);
這個(gè)如果放在嵌入式設(shè)備上運(yùn)行,用手觸摸屏幕也是一樣的效果,和用鼠標(biāo)操作是一樣的
到此,關(guān)于“qt事件過濾器如何使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站標(biāo)題:qt事件過濾器如何使用
轉(zhuǎn)載來于:http://aaarwkj.com/article38/pespsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、微信小程序、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)