Why does MouseArea sometimes determine that the mouse left the rectangular area to hide QML.Control is not successful?
-
import QtQuick 2.9 import QtQuick.Controls 2.3 import QtQuick.Window 2.2 ApplicationWindow { visible: true width: 640 height: 640 title: qsTr("Hello World") id:root // Item{ id:rctContains x:5 y:5 width:95 height:95 } Rectangle { id:testRct x:200 y:0 width:100 height:100 visible:false } Rectangle { id:rct x:0 y:0 width: 100 height: 100 color: "green" // MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onPositionChanged:{ if(rctContains.contains(Qt.point(mouse.x,mouse.y )) { testRct.visible = true } else { testRct.visible = false } } onExited: { } } }
-
@mirro said in Why does MouseArea sometimes determine that the mouse left the rectangular area to hide QML.Control is not successful?:
mouse has left all the submenus?
checking containsMouse property for every mousearea
-
Window { id: appWindow width: 512 height: 512 visible: true Rectangle{ width: 100 height: 100 color: "green" MouseArea{ id: mouseGreen anchors.fill: parent hoverEnabled: true } } Rectangle { id:testRct anchors.centerIn: parent width:100 height:100 color : "red" visible:mouseGreen.containsMouse } }
-
@LeLev Thank you.
There's a situation, I'm going to implement a function in the MouseArea of the control.Rectangle to hide the control.Rectangle when the mouse leaves.
However, there are Multiple child control.Rectangle on the control.Rectangle, how to avoid when the mouse moves to the child control.Rectangle, the control.Rectangle does not hide.
My idea was to figure out where the mouse was in the area of ‘rctContains’ to solve this problem, but the mouse would fail if it moved too fast.
I don't know if you have a solution.
import QtQuick 2.9 import QtQuick.Controls 2.3 import QtQuick.Window 2.2 ApplicationWindow { visible: true width: 640 height: 640 title: qsTr("Hello World") id:root // Item{ id:rctContains x:5 y:5 width:95 height:95 } Rectangle { id:rct x:0 y:0 width: 100 height: 100 color: "green" Rectangle{ id:subRct x:25 y:25 width:50 height:50 visible:false } Rectangle{ id:subRct2 x:25 y:50 width:75 height:75 visible:false } MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onPositionChanged:{ if(rctContains.contains(Qt.point(mouse.x,mouse.y )) { rct.visible = true } else { rct.visible = false } } onExited:{ } } } }
-
@mirro sorry i'm not sure i understand what do you mean..
And what is control.Rectangle ?
Could you explain more generally what are your trying to achieve ?
Note you can attach a MouseArea to every rectangle if you need
Your MouseArea is on the Rectangle rct if you hide it you will never be able to hover it again.Are you trying to make some kind of drop down menu ?
-
@LeLev control.Rectangle is mean QML.Control.Rectangle.
Yes, I want to hide the drop-down menu when the mouse leaves the rectangular area of the drop-down menu.
However, there is a problem. the drop-down menu hide when the mouse moves to the menuItem,
I don't know if you have a solution.
-
@mirro said in Why does MouseArea sometimes determine that the mouse left the rectangular area to hide QML.Control is not successful?:
control.Rectangle is mean QML.Control.Rectangle.
Still not sure what do you mean by that.. but lets just ignore this.
@mirro said in Why does MouseArea sometimes determine that the mouse left the rectangular area to hide QML.Control is not successful?:
Yes, I want to hide the drop-down menu when the mouse leaves the rectangular area of the drop-down menu.
That should be part of your initial question..
as i suggested before, add a MouseArea to each sub menu so you can test if there is one that contains mouse
or use Menu
https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-menu -
@mirro said in Why does MouseArea sometimes determine that the mouse left the rectangular area to hide QML.Control is not successful?:
mouse has left all the submenus?
checking containsMouse property for every mousearea
-
@LeLev
How to determine when the mouse leaves the Menu area to turn off Menu?
MenuItem is the topmost window, so it is impossible to determine if the mouse has left the Menu area.
import QtQuick 2.6 import QtQuick.Controls 2.1 Item{ // function openMenu() { menu.open() } function closeMenu() { menu.close() } Menu { id: menu background: Rectangle { implicitWidth: 200 implicitHeight: 200 color: "#ffffff" border.color: "#353637" } MenuItem { text: qsTr("New...") } MenuItem { text: qsTr("Open...") } MenuItem { text: qsTr("Save") } MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onExited: { menu.close() } } } }