Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidgetWindow as QObject in event filter with Qt5
Forum Updated to NodeBB v4.3 + New Features

QWidgetWindow as QObject in event filter with Qt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natxo
    wrote on 8 Dec 2017, 09:38 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 8 Dec 2017, 10:41 last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      N 1 Reply Last reply 11 Dec 2017, 10:11
      1
      • V VRonin
        8 Dec 2017, 10:41

        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

        N Offline
        N Offline
        Natxo
        wrote on 11 Dec 2017, 10:11 last edited by
        #3

        @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.

        1 Reply Last reply
        0

        1/3

        8 Dec 2017, 09:38

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved