S.M*_*avi 23 qt qml qt5 qtquick2
我想在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...
]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我有一个问题MouseArea.
Inner MouseArea(actionButton)对entered事件不起作用.当鼠标进入操作按钮时,外部MouseArea触发exited事件.
我的代码中有错误吗?更一般地说,我如何在QML中实现这样的场景?
iBe*_*eve 26
我遇到了同样的问题,并在QtQuick 5.0文档中MouseArea遇到了答案.答案实际上非常简单.
如果您想在父级中包含子鼠标悬停事件,请MouseArea让您的孩子MouseArea成为父级的子级MouseArea:
MouseArea {
id: parent
MouseArea {
id: child
}
}
Run Code Online (Sandbox Code Playgroud)
由于我有一个Widget可用作父视图的自定义类型,因此我最终将default属性作为以下内容的子项MouseArea:
Item {
default property alias children: mouseArea.data
MouseArea {
id: mouseArea
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一些东西,但似乎不可能MouseArea同时悬停在两个上面。在preventStealing和propagateComposedEvents当你有一个click事件似乎只工作。但是从内部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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是exited()来自包含的信号MouseArea将在entered()再次回调之前被调用。因此,您可能需要“延迟”状态更改,exited()以确保您确实想要隐藏操作按钮。另一种解决方案是保存当前鼠标位置并隐藏按钮,仅当exited()在其边框上使用鼠标调用时。
在视图中为元素的每个状态创建状态,然后可以使用诸如if语句或case语句之类的方法来更改这些属性。换句话说,请尝试不要将元素设置为可在MouseArea上使用,而是对属性进行设置,并将Elements属性设置为我希望在设置属性上工作,如果没有的话,我希望这是示例:
编辑我添加了颜色是透明的。如果没有鼠标的话。如果我使用的是图像,我会使用不透明度,然后添加一些行为,但这是可行的
例
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"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10298 次 |
| 最近记录: |