Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ code not receiving QQuickItem::wheelEvent()
Qt 6.11 is out! See what's new in the release blog

C++ code not receiving QQuickItem::wheelEvent()

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 822 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by Tom asso
    #1

    I’ve subclassed QQuickFrameBufferObject to QVtkItem and overridden wheelEvent() (originally defined in the QQuickItem base class):

    class QVtkItem : public QQuickFramebufferObject {
     
    protected:
     
       /// Handle mouse wheel event
       virtual void wheelEvent(QWheelEvent *event) override;
    

    When I instantiate a QVtkItem in QML, the item is visible but QVtkItem::wheelEvent() is never called when I put the mouse over the item and spin the wheel. I tried defining a MouseArea as a child, but QVtkItem::wheelEvent() is still not called. The QML looks like:

    QVtkItem {
         id: qVtkItem
         objectName: "qVtkItem"
         anchors.fill: parent
            width: 1000
            height: 1000
     
         MouseArea {
            acceptedButtons: Qt.LeftButton
            anchors.fill: parent
         }
    }
    

    What am I missing? QQuickItem documentation says this about wheelEvent(): “The event is accepted by default, so it is not necessary to explicitly accept the event if you reimplement this function."
    Thanks!

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      MouseArea will block events of the QtkItem. I just did a mock of this and the c++ event works.

      C++ is a perfectly valid school of magic.

      Tom assoT 1 Reply Last reply
      1
      • fcarneyF fcarney

        MouseArea will block events of the QtkItem. I just did a mock of this and the c++ event works.

        Tom assoT Offline
        Tom assoT Offline
        Tom asso
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by
          #4

          @fcarney - I did 'make clean' and 'make' and now QVtkItem::wheelEvent() is called - with or without the MouseArea. Thanks for your help!

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            I am doing onWheel event.
            main.qml:

            import QtQuick 2.15
            import QtQuick.Controls 2.12
            import QtQuick.Window 2.15
            import FBOs 1.0
            
            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Mouse Testing")
            
                FBO {
                    anchors.fill: parent
                }
            
                MouseArea {
                    id: mousearea
            
                    enabled: false // set to true and it blocks events from FBO
            
                    anchors.fill: parent
            
                    acceptedButtons: Qt.LeftButton
            
                    onWheel: {
                        console.log(wheel.x, wheel.y, wheel.angleDelta, wheel.pixelDelta)
                    }
                }
            
                Button {
                    text: "toggle mousearea: %1".arg(mousearea.enabled)
                    onClicked: {
                        mousearea.enabled = !mousearea.enabled
                    }
                }
            }
            

            main.cpp:

            #include <QGuiApplication>
            #include <QQmlApplicationEngine>
            #include <QQuickItem>
            #include <QQuickFramebufferObject>
            #include <QOpenGLFramebufferObjectFormat>
            
            class FBORenderer : public QQuickFramebufferObject::Renderer
            {
                QOpenGLFramebufferObject *createFramebufferObject(const QSize &size)
                {
                    QOpenGLFramebufferObjectFormat format;
                    format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
                    // optionally enable multisampling by doing format.setSamples(4);
                    return new QOpenGLFramebufferObject(size, format);
                }
            
                void render()
                {
                    // Called with the FBO bound and the viewport set.
                    // Issue OpenGL commands.
                }
            };
            
            class FBO : public QQuickFramebufferObject
            {
                Q_OBJECT
            public:
                FBO(){};
            
            protected:
                virtual void wheelEvent(QWheelEvent *event) override {
                    qInfo() << "c++:" << event->pos() << event->angleDelta();
                }
            
                QQuickFramebufferObject::Renderer *createRenderer() const
                {
                    return new FBORenderer;
                }
            };
            
            int main(int argc, char *argv[])
            {
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            
                QGuiApplication app(argc, argv);
            
                QQmlApplicationEngine engine;
            
                qmlRegisterType<FBO>("FBOs",1,0,"FBO");
            
                const QUrl url(QStringLiteral("qrc:/main.qml"));
                QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                 &app, [url](QObject *obj, const QUrl &objUrl) {
                    if (!obj && url == objUrl)
                        QCoreApplication::exit(-1);
                }, Qt::QueuedConnection);
                engine.load(url);
            
                return app.exec();
            }
            
            #include "main.moc"
            

            Does that make any difference?

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              If I do this:

                      onWheel: {
                          console.log(wheel.x, wheel.y, wheel.angleDelta, wheel.pixelDelta)
                          wheel.accepted = false
                      }
              

              Then I get both sets of printouts. So yeah, it ignores wheel in mousearea if you don't have onWheel callback defined. If I don't accept then it trigger wheel events on FBO object.

              C++ is a perfectly valid school of magic.

              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 Extensions
              • Unsolved