Qt eventFilter无法过滤mouse事件
来源:互联网 发布:国家税务局网络 编辑:程序博客网 时间:2024/05/29 09:13
最近做了个测试发现Qt 的eventFilter无法过滤mouse事件,如mouseMove, mousePress, mouseRelease等,
原来是installEventFilter(QObject *)安装不对。看Qt例子:
class MainWindow : public QMainWindow { public: MainWindow(); protected: bool eventFilter(QObject *obj, QEvent *ev); private: QTextEdit *textEdit; }; MainWindow::MainWindow() { textEdit = new QTextEdit; setCentralWidget(textEdit); textEdit->installEventFilter(this); } bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (obj == textEdit) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); qDebug() << "Ate key press" << keyEvent->key(); return true; } else { return false; } } else { // pass the event on to the parent class return QMainWindow::eventFilter(obj, event); } }这里eventFilter接受的是QEvent::KeyPress事件,打印event->type发现始终获得不到mouse事件,
原来因为QTextEdit是由QAbstractScollArea派生的,对于QAbstractScollArea,其过滤器要安装在viewport上,
因此上面程序变换如下即可过滤mouse event:
class MainWindow : public QMainWindow { public: MainWindow(); protected: bool eventFilter(QObject *obj, QEvent *ev); private: QTextEdit *textEdit; }; MainWindow::MainWindow() { textEdit = new QTextEdit; setCentralWidget(textEdit); textEdit->installEventFilter(this); textEdit->viewPort->installEventFilter(this); } bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (obj == textEdit) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); qDebug() << "Ate key press" << keyEvent->key(); return true; } else { return false; } } else if (obj == textEdit->viewPort()) { // To do..... if (event->type() == QEvent::mouseMove) { return true; } } else { // pass the event on to the parent class return QMainWindow::eventFilter(obj, event); } }
- Qt eventFilter无法过滤mouse事件
- Qt中EventFilter过滤鼠标事件
- 使用EventFilter进行事件过滤
- 一起看代码来玩玩QT之06EventFilter (事件过滤)
- Qt Focus事件处理及eventFilter的用法
- Qt Focus事件处理及eventFilter的用法
- Qt之eventFilter
- 诡异的Qt eventFilter
- [转]事件过滤器 eventFilter
- Qt: 事件过滤
- Qt事件过滤
- <Qt+Linux>事件过滤
- Qt事件过滤
- Qt事件过滤
- 【QT】事件过滤
- QT中eventFilter的使用方法
- QT中eventFilter的使用方法
- qt 为什么不能响应eventFilter
- ActiveX控件的MFC设计之旅-第7步 .
- 《开学第一课》观后感——幸福
- linux设备驱动学习(11) linux设备模型2
- ActiveX控件的MFC设计之旅-第8步 .
- 第一天
- Qt eventFilter无法过滤mouse事件
- linux设备驱动学习(11) linux设备模型3
- ActiveX控件的MFC设计之旅-第9步 .
- openfire服务器端增加聊过天的记录(openfire源码修改)
- ActiveX控件的MFC设计之旅-第10步 .
- 日期循环扫描是个常用的东西。perl简单的日期循环【备用】
- 关于selinux对php调用shell的限制
- 使用Profiler找出低效的查询
- ActiveX控件的MFC设计之旅-第11步 .