利用installEventFilter安裝全局事件過濾器


 

www.qt-ui.com 原創技術文章

當我們需要處理窗口以外的全局鍵盤和鼠標事件的時候,我們可以通過installEventFilter在QApplication上安裝事件過濾器來進行處理。

調用示例如下:

UIGQtPOSLogin::UIGQtPOSLogin(QWidget *parent)
    : UIGQtLib::UIGQWindow(parent)
{
    UIGQtLib::uigCreatePageByFileName(this, "LoginPage.json"); _closeBtn = findChild<UIGQtLib::UIGQPushButton*>("btnLogin"); _editUser = findChild<UIGQtLib::UIGQLineEdit*>("editUser"); _editUser->setTextMargins(20, 0, 20, 0); connect(_closeBtn, SIGNAL(clicked()), this, SLOT(BtnClicked())); setWindowFlag(Qt::FramelessWindowHint); QApplication::instance()->installEventFilter(this); } 

這里我們利用QApplication::instance()->installEventFilter(this);完成了全局過濾器的安裝。

然后我們實現一下eventFilter,來接收全局的事件消息。

bool UIGQtPOSLogin::eventFilter(QObject * pObject, QEvent * pEvent) { if (pEvent->type() == QEvent::KeyRelease) { QKeyEvent* pKeyEvt = (QKeyEvent*)pEvent; int key = pKeyEvt->key(); ... } return UIGQtLib::UIGQWindow::eventFilter(pObject, pEvent); }

 

 

 

更多詳情請瀏覽

www.qt-ui.com

www.qt-ui.com.cn

 

 

 Qt-UI C++Python界面開發工具 UI開發工具 qt解決方案 qt開源項目

更多詳情請瀏覽

www.qt-ui.com

www.qt-ui.com.cn

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM