C++ code not receiving QQuickItem::wheelEvent()
-
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! -
MouseArea will block events of the QtkItem. I just did a mock of this and the c++ event works.
-
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?
-
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.