Qt eventfilter cannot filter mouse events

Source: Internet
Author: User

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);    } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.