Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQuickItem overrides and calling base method
Qt 6.12 LTS is out! See what's new in the release blog

QQuickItem overrides and calling base method

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 929 Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bob64
    wrote on last edited by Bob64
    #1

    I am currently re-implementing some code, originally done in QWidgets, in QML. This involved rendering pixmaps using QWidget::paintEvent. The code seems to translate over to QML pretty well using QQuickPaintedItem.

    Using the QQuickItem API directly is fairly new to me and one thing that threw me was that if I followed what was done too blindly in the QWidget version with regard to mouse event handling, the QML version was not responding to certain mouse operations. In particular, all of the overrides of QWidget::mousePressEvent/mouseReleaseEvent/mouseMoveEvent... end by delegating to the base method:

    void MyWidget::mousePressEvent(QMouseEvent* evt) {
        ... // my override stuff
        QWidget::mousePressEvent(evt);
    }
    

    When I followed this pattern in the QQuickPaintedItem version, I found that mousePressEvent was triggered, but not mouseMoveEvent and mouseReleaseEvent.

    Looking in the Qt source, I see that the default QQuickItem::mousePressEvent implementation calls ignore() on the event, which (I think) explains what I was seeing. This is not spelled out in the documentation as far as I can see.

    My question is: as a general rule of thumb, if I override a QQuickItem protected method for mouse, key, wheel events, or whatever, do I just ignore the base method, or are there situations where I should call it like I see happening everywhere in the QWidget code?

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      The documentation is wrong.

      At a glance, the following QQuickItem event handler implementations are incorrectly documented as accepting the event, because the event is explicitly ignored:

      QQuickItem::keyPressEvent
      QQuickItem::keyReleaseEvent
      QQuickItem::inputMethodEvent
      QQuickItem::mousePressEvent
      QQuickItem::mouseMoveEvent
      QQuickItem::mouseReleaseEvent
      QQuickItem::wheelEvent
      QQuickItem::touchEvent

      Fortunately, none of these do any useful work. Overrides can either not call them, or accept the event after. The documentation usually mentions which base class functions should be called when overridden:

      https://doc.qt.io/qt-6/qquickitem.html#focusInEvent

      If you do reimplement this function, you should call the base class implementation.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      B 1 Reply Last reply
      1
      • jeremy_kJ jeremy_k

        The documentation is wrong.

        At a glance, the following QQuickItem event handler implementations are incorrectly documented as accepting the event, because the event is explicitly ignored:

        QQuickItem::keyPressEvent
        QQuickItem::keyReleaseEvent
        QQuickItem::inputMethodEvent
        QQuickItem::mousePressEvent
        QQuickItem::mouseMoveEvent
        QQuickItem::mouseReleaseEvent
        QQuickItem::wheelEvent
        QQuickItem::touchEvent

        Fortunately, none of these do any useful work. Overrides can either not call them, or accept the event after. The documentation usually mentions which base class functions should be called when overridden:

        https://doc.qt.io/qt-6/qquickitem.html#focusInEvent

        If you do reimplement this function, you should call the base class implementation.

        B Offline
        B Offline
        Bob64
        wrote on last edited by
        #3

        @jeremy_k thanks, yes - I think at best the documentation is ambiguous. It says that by default the event is accepted - presumably before the handler is called - so that one does not need to explicitly accept it if the handler is overridden. It doesn't actually say one way or the other what the base implementation does but I think a quick reading of the documentation would give an impression that the event is accepted.

        jeremy_kJ 1 Reply Last reply
        0
        • B Bob64

          @jeremy_k thanks, yes - I think at best the documentation is ambiguous. It says that by default the event is accepted - presumably before the handler is called - so that one does not need to explicitly accept it if the handler is overridden. It doesn't actually say one way or the other what the base implementation does but I think a quick reading of the documentation would give an impression that the event is accepted.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @Bob64 said in QQuickItem overrides and calling base method:

          It says that by default the event is accepted - presumably before the handler is called - so that one does not need to explicitly accept it if the handler is overridden.

          The handlers are ordinary C++ virtual functions, called from QQuickItem::event(). If event() or the relevant *Event() is overridden, it's up to the override to invoke the default behavior. Otherwise, it doesn't happen.

          https://codebrowser.dev/qt6/qtdeclarative/src/quick/items/qquickitem.cpp.html#8879

              case QEvent::MouseButtonPress:
                  mousePressEvent(static_cast<QMouseEvent*>(ev));
          

          It doesn't actually say one way or the other what the base implementation does but I think a quick reading of the documentation would give an impression that the event is accepted.

          Some of the documentation is more thorough, and some of it is in higher level descriptions. QWidget::mousePressEvent, for example:

          The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved