內部MouseArea
首先獲取鼠標事件。我想「看到」這些事件,以便設置各種屬性,但不會影響它們。我想將鼠標事件傳播給任何父母MouseArea
。qt qml。 MouseArea可以看到事件,但是將它們全部傳遞給父項而不影響它們?
考慮這個代碼。我想點擊藍色方塊看到「藍色按下」和「藍色釋放」,並傳遞給「父母按下」和「父母釋放」。
如果我接受該事件,父母不會得到它。如果我不接受按下,那麼我不會看到發佈。
import QtQuick 2.7
import QtQuick.Controls 1.4
ApplicationWindow
{
visible: true
width: 800
height: 1024
Rectangle
{
anchors.fill: parent
color: "yellow"
MouseArea
{
// i want these to happen even when mouse events are in the
// blue square
anchors.fill: parent
onPressed: console.log("parent pressed");
onReleased: console.log("parent released");
}
Rectangle
{
x: 100
y: 100
width: 100
height: 100
color: "blue"
// i would like to "see" events, but not affect them
// i want all mouse events to pass to parent, as if i am not here.
// however, not accepting "pressed" means i don't see "released"
MouseArea
{
anchors.fill: parent
onPressed:
{
console.log("blue pressed");
mouse.accepted = false
}
onReleased:
{
console.log("blue released");
mouse.accepted = false
}
}
}
}
}
想法歡迎。謝謝,
根據文檔,設置'mouse.accepted'沒有效果。你可以改爲使用'onCancelled',如[這裏](http://doc.qt.io/qt-5/qml-qtquick-mousearea.html#canceled-signal) – DuKes0mE
中所描述的那樣,也可以複製到:http:/ /stackoverflow.com/questions/23911433/qml-cant-get-mouse-released-event-when-i-dont-accept-mouse-pressed-event – DuKes0mE
'onCancelled'無助於追蹤'onReleased'時另一個' MouseArea'聲明信號。另一個答案可能是對的,但可能會更清楚:*「帶有'MouseArea'這是不可能的」*。其餘的*在他看來純粹是基於觀點的,甚至沒有試圖解決問題*。相反,他的解決方案包括靜態UI,其中所有職位都是預先確定的,或者您需要跟蹤職位。至少MouseArea1需要了解MouseArea2的用途和狀態。我不認爲這是可取的。 – derM