QWidgetWindow as QObject in event filter with Qt5



  • Hello,
    in Qt4 I was using event filtering for whole QApplication by calling

    @application->installEventFilter(this)@

    supposing "this" has implemented function

    @bool eventFilter(QObject * o, QEvent * e )@

    In this eventFilter function I was comparing this QObject * o with some another QWidget *, because the goal was to filter every event for every widget, excluding some selected widgets.

    But after the conversion to Qt5 the QObject * o, which is passed to eventFilter function is no longer of type QWidget as it was before, but is instead of type QWidgetWindow, which has this QWidget as a private member. Also, this QWidgetWindow and its header seems to me to be some sort of private class not intended for including in user code. Has someone any suggestions, how to solve this problem? Is there a way how to get the "original" QWidget from QWidgetWindow? Or is there some other way of knowing for which widget is event intended?

    Any help will be very appreciated and I kindly thank you in advance.
    Jurik



  • Maybe @bool QObject::isWidgetType() const@ or @bool QObject::isWindowType() const@ can help.



  • I don't think that he's talking about testing if passed QObject is QWidget or not. He is talking about change in installed event filter: It does not pass QWidget anymore but QWidgetWindow which does not publish QWidget for which event is currently in installed event filter.If it's so, then it's pretty ugly change in Qt5. I'm using Qt4 in same way, but won't go to Qt5 if it's so.
    @Jurik, but QWidget have windowHandle() in Qt5 ? If you iterate through widgets then ask for it's window handle and compare it with QObject from event.



  • You probably found the answer you are looking for by now. But for others, this is what I'm seeing.

    The eventFilter() method is getting called several times for each event (as the event passes through the tree). The first call the object is a QWidgetWindow, the second call it's the object I'm looking for. So, try something like the following. Not the best code, but you should get the idea.

    @
    bool MyEvtFilter::eventFilter(QObject * o, QEvent * e)
    {
    if ((e->type() == QMouseEvent::MouseButtonPress) ||
    (e->type() == QMouseEvent::MouseButtonRelease))
    {
    if (o->inherits("QWidgetWindow") )
    {
    // ignore these
    return QObject::eventFilter(o, e);
    }
    else if (o->inherits("MySpecialWidget") )
    {
    // do my stuff
    return true;
    }
    }
    return QObject::eventFilter(o, e);
    }
    @



  • You probably found the answer you are looking for by now. But for others, this is what I'm seeing.

    The eventFilter() method is getting called several times for each event (as the event passes through the tree). The first call the object is a QWidgetWindow, the second call it's the object I'm looking for. So, try something like the following. Not the best code, but you should get the idea.

    @
    bool MyEvtFilter::eventFilter(QObject * o, QEvent * e)
    {
    if ((e->type() == QMouseEvent::MouseButtonPress) ||
    (e->type() == QMouseEvent::MouseButtonRelease))
    {
    if (o->inherits("QWidgetWindow") )
    {
    // ignore these
    return QObject::eventFilter(o, e);
    }
    else if (o->inherits("MySpecialWidget") )
    {
    // do my stuff
    return true;
    }
    }
    return QObject::eventFilter(o, e);
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.