我想在QML中实现以下场景。
以下是ListView
元素的示例/简化委托:
Component {
Item {
id: container
MouseArea {
anchors.fill: parent
hoverEnabled: true
onClicked: {
container.ListView.view.currentIndex = index
container.forceActiveFocus();
}
onEntered: {
actionList.state = "SHOW";
myItem.state = "HOVER"
}
onExited: {
actionList.state = "HIDE";
myItem.state = "NORMAL"
}
Rectangle {
id: myItem
color: "gray"
anchors.fill: parent
Row {
id: actionList
spacing: 5; anchors.fill: parent
Image {
id: helpAction
source: "" //Some image address
width: 16; height: 16; fillMode: Image.PreserveAspectFit
states: [
State {
name: "NORMAL"
PropertyChanges { target: helpAction; opacity: 0.7 }
},
State {
name: "HOVER"
PropertyChanges { target: helpAction; opacity: 1.0 }
}
]
MouseArea {
hoverEnabled: true
anchors.fill: parent
onEntered: {
parent.state = "HOVER";
}
onExited: {
parent.state = "NORMAL";
}
}
states: [
State {
name: "SHOW"
PropertyChanges { target: actionList; visible: false }
},
State {
name: "HIDE"
PropertyChanges { target: actionList; visible: true }
}
]
}
//Other action buttons...
states: [
// `NORMAL` and `HOVER` states definition here...
]
}
}
}
}
但我对MouseArea
有疑问
内部MouseArea
(actionButton)无法正常处理entered
事件。当鼠标进入操作按钮时,外部MouseArea
会触发exited
事件。
我的代码中有错误吗?更一般地说,我如何在QML中实现这样的场景?
答案 0 :(得分:23)
我遇到了同样的问题,并在QtQuick 5.0 documentation for MouseArea
中遇到了答案。答案实际上非常简单。
如果您想在您的父MouseArea
中添加子鼠标悬停事件,请让您的孩子MouseArea
成为父MouseArea
的孩子:
MouseArea {
id: parent
MouseArea {
id: child
}
}
由于我有一个自定义Widget
类型可用作父视图,因此我最终将default
属性作为MouseArea
的子项:
Item {
default property alias children: mouseArea.data
MouseArea {
id: mouseArea
}
}
答案 1 :(得分:4)
为视图中元素的每个状态创建状态,然后您可以使用if语句或case语句等内容来更改这些属性。换句话说,尽量不要将元素设置为在MouseArea上工作但在属性上设置并设置要处理set属性的元素属性我希望这有助于(如果不是)示例:
编辑我添加的颜色是透明的。如果没有老鼠那么多。如果我使用的是Image,我会使用不透明度,然后添加一堆行为,但这是一个有效的
例如
import QtQuick 2.0
Rectangle {
width: 360
height: 360
property string state1:"OutMouse"
property string state2: "OutMouse"
property string state3: "OutMouse"
property string state4: "OutMouse"
Rectangle{
id:blueRec
width: parent.width
height: parent.height / 6
color: state1 === "InMouse" ? "blue" : "green"
MouseArea{
anchors.fill: blueRec
hoverEnabled: true
onEntered: state1 = "InMouse"
onExited: {
if (state1 === state2 || state3 || state4){
state1 = "InMouse"
}
if(state1 !== state2 || state3 || state4)
{
state1 = "OutMouse"
}
}
}
Text {
text: state1=== "InMouse"? qsTr("foo") :"bar"
anchors.centerIn: blueRec
}
Row{
width: parent.width
height: parent.height / 4
spacing: 2
anchors{
left: parent.left
verticalCenter: blueRec.verticalCenter
leftMargin: blueRec.width / 12
}
Rectangle{
id: rec1
height: parent.height;
width: height
color: {
if ( state3 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"}
MouseArea{
id: rec1M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state1 = "InMouse"
state2 = "InMouse"
}
onExited: state2 = "OutMouse"
}
}
Rectangle{
id: rec2
height: parent.height ;
width: height
color: {
if (state3 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"
}
MouseArea{
id: rec2M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state1 = "InMouse"
state3 = "InMouse"
}
onExited: state3 = "OutMouse"
}
}
Rectangle{
id: rec3
height: parent.height;
width: height
color:{
if (state4 === "InMouse")
return "gray"
if (state1 === "OutMouse")
return "transparent"
else
return "white"
}
MouseArea{
id: rec3M
anchors.fill: parent
hoverEnabled: true
onEntered:{
state4 = "InMouse"
state1 = "InMouse"
}
onExited: state4 = "OutMouse"
}
}
}
}
}
答案 2 :(得分:3)
我试过了一些事情,但似乎不可能同时悬停在两个MouseArea
上。 preventStealing
和propagateComposedEvents
似乎仅在您有点击事件时才有效。但是从内部MouseArea
你可以触发另一个entered()
信号。像这样:
import QtQuick 2.1
Rectangle {
width: 500
height: 500
Rectangle {
width:300
height: 300
color: "red"
MouseArea {
id: big
anchors.fill: parent
hoverEnabled:true
onEntered: {
console.log("ENTERED BIG mousearea");
}
onExited: {
console.log("EXITED BIG mousearea");
}
}
Rectangle {
anchors.centerIn: parent
height: 100
width: 100
color: "green"
MouseArea {
anchors.fill: parent
hoverEnabled:true
onEntered: {
console.log("ENTERED small mousearea");
big.entered();
}
onExited: {
console.log("EXITED small mousearea");
big.exited();
}
}
}
}
}
问题是,在再次调用exited()
之前,将调用包含MouseArea
的{{1}}信号。因此,您可能需要“延迟”entered()
中的状态更改,以确保您确实要隐藏操作按钮。另一个解决方案是保存当前鼠标位置并仅在使用鼠标在其边框上调用exited()
时隐藏按钮。
答案 3 :(得分:1)
试试这个:
两者上的鼠标退出仍将取消悬停状态。当您将鼠标移离控件时,它应该可以正常工作而无需任何额外的代码