Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] C++ QQuickItem with QML MouseArea doesn't behave as expected



  • Hello,

    In a C++ QQuickItem derived class, I'm creating a QML Item the following way.

    @
    QQmlEngine engine;
    QQmlComponent component(&engine);
    component.setData("import QtQuick 2.0 \n"
    "Rectangle { "
    " width:200; "
    " height:100;"
    " color : rectMA.containsMouse ? 'red' : 'blue';"
    " MouseArea { "
    " id : rectMA;"
    " anchors.fill: parent;"
    " drag.target: parent; "
    " hoverEnabled: true; "
    " onClicked : console.log('clicked');"
    " }"
    "}", QUrl());

    QQuickItem * item = qobject_cast<QQuickItem *>(component.create());
    item->setParentItem(this);
    item->setAcceptedMouseButtons(Qt::AllButtons);
    

    @

    The MouseArea doesn't behave as expected:

    • drag is perfectly working
    • onClicked : console.log('clicked'); returns nothing
    • color : rectMA.containsMouse ? 'red' : 'blue';" has no effect. It should when hovered
    • However, the hover is recognized (I don't explain here why)

    Using @component.loadUrl (QUrl::fromLocalFile("myFile.qml")@ instead of setData has the same effect (doesn"t work as expected)

    Of course, if I use this "myFile.qml" as a pure QML without C++, it works

    Anybody has an Idea ??

    Thanks in advance
    Laurent



  • Solved :

    It works if I reuse the QQmlEngine of the parent QQmlViewer.
    Here, the QQmlEngine was locally created.


Log in to reply