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. [Solved] C++ QQuickItem with QML MouseArea doesn't behave as expected
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.7k 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.
  • L Offline
    L Offline
    laurent bauer
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      laurent bauer
      wrote on last edited by
      #2

      Solved :

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

      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