0

I want to create a QML item which disappears when the mouse moves outside of it. Here is my code:

Item {
  id: disappearing_element

  ListView { ... }

  MouseArea {
    id: collapser
    anchors.fill: parent
    propagateComposedEvents: true
    hoverEnabled: true

    onExited: {
      disappearing_element.visible = false
    }
  }
}

It works well, but MouseArea propagates events like onClicked() onDoubleClicked() only (as said in Qt docs).

Is there a way to notify disappearing_element's childrens about mouse enter and mouse exit events (without using a Popup element)?

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • You can use `disappearing_element.visible` for notifications to the children by binding to it, i.e. `property bool someProp : parent.visible; onSomePropChanged: { ...your stuff... }` – dtech Aug 19 '16 at 09:29
  • You misunderstand me. I want to propagate MouseEvent to `disappearing_element`'s childrens to check if one of them contains mouse cursor or not. – Deedee Megadoodoo Aug 19 '16 at 09:33
  • Did you try using the `Item itemAt(int x, int y)` function of `ListView`? – dtech Aug 19 '16 at 09:35
  • List elements in QML are not direct children of the view, there is a `contentItem` in between. – dtech Aug 19 '16 at 09:37
  • Possible duplicate of [How to include child mouse hover events in the parent MouseArea using QML?](http://stackoverflow.com/questions/18135262/how-to-include-child-mouse-hover-events-in-the-parent-mousearea-using-qml) – S.M.Mousavi Aug 27 '16 at 07:45
  • See [this answer](http://stackoverflow.com/questions/18135262/how-to-include-child-mouse-hover-events-in-the-parent-mousearea-using-qml). – S.M.Mousavi Aug 27 '16 at 07:46

1 Answers1

1

I think this is one of the common needs when developing QtQuick apps. One solution we currently use quite often is to add MouseArea in each of the children that need check mouse containment, and emit signals (and catch these signals in your main item) when the mouse enters or exits.

Things go a bit complicated when the children items also need such mechanism to manage their children. However, for common usage, this approach is enough for us right now.

Jimmy Chen
  • 490
  • 3
  • 13