我想在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中实现这样的情景?
MouseArea
放在外部的MouseArea
里面。双重检查hoverEnabled: true
,它就会工作了。 - S.M.MousaviCheckBox
位于列表的一行上,在此列表行上悬停时会突出显示。起初,我将CheckBox
和MouseArea
放在同一级别,但是一旦将CheckBox
嵌套在MouseArea
内,并且将hoverEnabled: true
应用于封闭的MouseArea
,这将为CheckBox
启用悬停效果,此答案在运行Qt5.9时有效。 - Kasheen