A recent test showed that eventfilter of QT could not filter mouse events, such as mousemove, mousepress, and mouserelease,
It turns out that the installation of installeventfilter (qobject *) is incorrect. See the QT example:
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 accepts the qevent: keypress event, prints the event-> type, and finds that the mouse event is never obtained,
It turns out that qtextedit is derived from qiniactscollarea. For qiniactscollarea, the filter must be installed on the viewport,
Therefore, the above program transformation is as follows to filter the 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); } }