SGURL
SGURL

Reputation: 87

QWidget seems to ignore event passed by EventFilter

I got a problem when trying to pass events with the QT eventFilter.

bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if(event->type() == QEvent::Wheel){
    QPoint pos = QCursor::pos();
    QWidget *widget = QApplication::widgetAt(pos);
        if(widget != NULL){
            widget->setVisible(false); //for Test purposes only
            qDebug() << widget; //also for test 
            QApplication::sendEvent(widget, event); //should send event to widget?
                return true;
            }

                return true;
        }
    return false;
}

I want to catch the scrolling of the Mouse-wheel and pass it to multiple QWidgets at once. If I execute the above without the sendEvent the widget i want to attend will disappear (thats what i kinda want for the test). If i use the sendEvent, nothing happens OR it crashes with an segmentation fault error... I tried anything i found online (or I at least think so)..

Thanks in advance!!!

Upvotes: 1

Views: 1441

Answers (1)

pat
pat

Reputation: 497

When you call QApplication::sendEvent(widget, event); and if widget is a child of MainWindow or itself, the receiving event will be filtered again in MainWindow::eventFilter. It creates a recursive infinite loop. I added a flag sendingEvent to return true; if event is sending. Hope that can help.

bool MyWindow::eventFilter(QObject *obj, QEvent *event){

    if (event->type() == QEvent::Wheel)
    {
        static bool sendingEvent = false;
        if (sendingEvent)
        {
            //exit recursive call, return false to avoid swallowing the event
            return false;
        }
        QPoint pos = QCursor::pos();
        QWidget *widget = QApplication::widgetAt(pos);
        if (widget != NULL){
            qDebug() << widget; //also for test         

            //set the flag before sending event
            sendingEvent = true;

            QApplication::sendEvent(widget, event); //should send event to widget?

            //reset the flag after sending event
            sendingEvent = false;
            return true;
        }
        return true;
    }
    return false;
}

Upvotes: 2

Related Questions