QML Mousearea onReleased 未发出
QML Mousearea onReleased not emitted
我正在尝试在 QML 的矩形中使用 Mousearea。我正在使用 onPressed、OnCLicked、onDoubleClicked 和 onPositionChanged。
但是,QML 没有收到 onReleased 事件。你们能告诉我,为什么它不起作用吗?我的代码如下:
Rectangle {
width: parent.width
height: 500
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
preventStealing: true
onPositionChanged: {
// console.log("onPositionChanged mouse X", mouse.x)
// console.log("onPositionChanged mouse Y", mouse.y)
mouse.accepted = false;
}
onClicked:{
console.log("onClicked mouse X", mouse.x)
console.log("onClicked mouse Y", mouse.y)
mouse.accepted = false;
}
onDoubleClicked:{
console.log("onDoubleClicked mouse X", mouse.x)
console.log("onDoubleClicked mouse Y", mouse.y)
mouse.accepted = false;
}
onExited:{
console.log("onExited")
}
onPressAndHold:{
console.log("onPressAndHold mouse X", mouse.x)
console.log("onPressAndHold mouse Y", mouse.y)
mouse.accepted = false;
}
onPressed:{
console.log("onPressed mouse X", mouse.x)
console.log("onPressed mouse Y", mouse.y)
mouse.accepted = false;
}
onReleased:{
console.log("onReleased mouse X", mouse.x)
console.log("onReleased mouse Y", mouse.y)
mouse.accepted = false;
}
onCanceled:{
console.log("onCanceled mouse X", mouse.x)
console.log("onCanceled mouse Y", mouse.y)
mouse.accepted = false;
}
}
}
根据此 docs 中的事件行为。
When handling this signal, changing the accepted property of the mouse parameter has no effect.
因此,如果您在上面的代码中注释 mouse.accepted = false;
行,它将无法正常工作。
下面的工作代码
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
preventStealing: true
onReleased:{
console.log("onReleased mouse X", mouse.x)
console.log("onReleased mouse Y", mouse.y)
}
}
如果您附加一个清除 mouse.accepted 的 onPressed 处理程序,则 MouseArea 假设如果您对新闻不感兴趣,您对任何后续相关信号也不感兴趣,因此它不会发出点击、按住或松开。另一方面,如果您保留 mouse.accepted 设置,则其他较低的 MouseArea 对象将看不到任何消息。不幸的是,这使得无法监视由较低 MouseArea 处理的所有信号。
我正在尝试在 QML 的矩形中使用 Mousearea。我正在使用 onPressed、OnCLicked、onDoubleClicked 和 onPositionChanged。
但是,QML 没有收到 onReleased 事件。你们能告诉我,为什么它不起作用吗?我的代码如下:
Rectangle {
width: parent.width
height: 500
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
preventStealing: true
onPositionChanged: {
// console.log("onPositionChanged mouse X", mouse.x)
// console.log("onPositionChanged mouse Y", mouse.y)
mouse.accepted = false;
}
onClicked:{
console.log("onClicked mouse X", mouse.x)
console.log("onClicked mouse Y", mouse.y)
mouse.accepted = false;
}
onDoubleClicked:{
console.log("onDoubleClicked mouse X", mouse.x)
console.log("onDoubleClicked mouse Y", mouse.y)
mouse.accepted = false;
}
onExited:{
console.log("onExited")
}
onPressAndHold:{
console.log("onPressAndHold mouse X", mouse.x)
console.log("onPressAndHold mouse Y", mouse.y)
mouse.accepted = false;
}
onPressed:{
console.log("onPressed mouse X", mouse.x)
console.log("onPressed mouse Y", mouse.y)
mouse.accepted = false;
}
onReleased:{
console.log("onReleased mouse X", mouse.x)
console.log("onReleased mouse Y", mouse.y)
mouse.accepted = false;
}
onCanceled:{
console.log("onCanceled mouse X", mouse.x)
console.log("onCanceled mouse Y", mouse.y)
mouse.accepted = false;
}
}
}
根据此 docs 中的事件行为。
When handling this signal, changing the accepted property of the mouse parameter has no effect.
因此,如果您在上面的代码中注释 mouse.accepted = false;
行,它将无法正常工作。
下面的工作代码
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
preventStealing: true
onReleased:{
console.log("onReleased mouse X", mouse.x)
console.log("onReleased mouse Y", mouse.y)
}
}
如果您附加一个清除 mouse.accepted 的 onPressed 处理程序,则 MouseArea 假设如果您对新闻不感兴趣,您对任何后续相关信号也不感兴趣,因此它不会发出点击、按住或松开。另一方面,如果您保留 mouse.accepted 设置,则其他较低的 MouseArea 对象将看不到任何消息。不幸的是,这使得无法监视由较低 MouseArea 处理的所有信号。