利用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