QWidgetWindow as QObject in event filter with Qt5



  • Re: QWidgetWindow as QObject in event filter with Qt5

    Hello,

    I'm opening a new thread following the suggestions of forum's policy, but the issue I have it's the same as in the post i'm replying:

    In the migration from Qt4 to Qt5 some code I have to filter events on my application does not work anymore. My software has to filter all events when in "Edition Mode" so only a few widgets/buttons can be available/clickable.

    I have a class "InputGrabber" with a QVector of Pointers to QWidget and an eventFilter function. When passing to "Edit" mode , I append the widgets' pointers to the QVector of my InputGrabber that will be available and then, on the eventFilter function the code goes like:

    bool InputGrabber::eventFilter (QObject* object, QEvent* anevent)
    {
    if (QInputEvent* event = dynamic_cast <QInputEvent*> (anevent))
      {
        QWidget* widget = dynamic_cast <QWidget*> (object);
        if (widget)
        {
          if (_widgetList.contains (widget) || _widgetList.contains (widget->parentWidget ()))
            return QObject::eventFilter (object, event);
          else if (QDialog* dlg = dynamic_cast <QDialog*> (widget))
            return QObject::eventFilter (object, event);
          else if (QDialogButtonBox* dlg = dynamic_cast <QDialogButtonBox*> (widget->parentWidget ()))
            return QObject::eventFilter (object, event);
          else
            return true;
        }
        else
          return true;
      }
      else
        return QObject::eventFilter (object, event);
    } 
    }
    

    The issue migrating to Qt5 is that the
    QWidget* widget = dynamic_cast <QWidget*> (object);
    cast returns NULL when object inherits from "QWidgetWindow"

    Has anyone found same issue and got a solution that follows the same strategy ?? I wouldn't like to change the behaviour of my software , making loops on all widgets and setting setDisabled() for each one...

    Thank you very much!



  • QWidgetWindow is not a QWidget so you can't cast it.
    you could cast it to QWindow but I'm not sure it helps with your current algorithm. You could change _widgetList to `QVector<QObject*> if it doesn't break other parts.

    P.S.
    When using QObject don't use dynamic_cast but qobject_cast



  • @VRonin
    Thanks a lot for your return.
    In the future I'll be using qobject_cast as suggested, and as QWidgetWindow is a "private" class used in the implementation of Qt, I don't advice people to 'play' with it. As said in its header file:

    // This file is not part of the Qt API. It exists purely as an
    // implementation detail. This header file may change from version to
    // version without notice, or even be removed.

    I didn't check your advice of using a QVector of QObject pointers, but I also thought about it. Finally I changed the logic: if the cast of the widget fails, I don't stop the event to propage , but I let it 'live' free:

    if (widget)
    {
    //the code above
    }
     else
          return QObject::eventFilter (object, event);
    

    And now it works as espected.


Log in to reply
 

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