Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-42194

Provide a way to force re-evaluation of MouseArea's containsMouse property

XMLWordPrintable

    • 6f84a09dfbd15aac023580cf06e7b8c24f3b524c

      It is documented that a MouseArea's containsMouse property is only updated when the mouse actually moves:

      Warning: This property is not updated if the area moves under the mouse: containsMouse will not change. In addition, if hoverEnabled is false, containsMouse will only be valid when the mouse is pressed while the mouse cursor is inside the MouseArea.

      This presents problems for scenarios like the one below:

      import QtQuick 2.4
      import QtQuick.Controls 1.3
      
      Rectangle {
          id: mainMenu
          activeFocusOnTab: false
          width: 650
          height: 600
      
          ListView {
              id: listView
              anchors.centerIn: parent
              width: 200
              height: 300
              model: ListModel {
                  Component.onCompleted: {
                      for (var i = 0; i < 10; ++i) {
                          append({name: i + 1});
                      }
                  }
              }
      
              delegate: Rectangle {
                  width: listView.width
                  height: 30
                  color: "black"
      
                  Text {
                      text: modelData
                      color: mouseArea.containsMouse ? "white" : "grey"
                      anchors.centerIn: parent
                  }
      
                  MouseArea {
                      id: mouseArea
                      anchors.fill: parent
                      hoverEnabled: true
                      onClicked: listView.model.remove(index)
                  }
              }
          }
      }
      

      The user would expect that by removing an item from the list, the item that takes the removed item's place (if any) would be highlighted, but it's not.

      There might not be a declarative way to solve this problem, but we could provide a function that simply checks the last mouse position and updates containsMouse on demand:

          // ...
      
          ListView {
              // ...
      
              delegate: Rectangle {
                  // ...
      
                  MouseArea {
                      id: mouseArea
                      anchors.fill: parent
                      hoverEnabled: true
                      onClicked: listView.model.remove(index)
      
                      Connections {
                          target: listView
                          onCountChanged: mouseArea.refresh()
                          // or: onCountChanged: mouseArea.refreshContainsMouse()
                          // or: onCountChanged: mouseArea.checkIfContainsMouse()
                      }
                  }
              }
          }
      }
      

        For Gerrit Dashboard: QTBUG-42194
        # Subject Branch Project Status CR V

            srutledg Shawn Rutledge
            mitch_curtis Mitch Curtis
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes