我有一個StackView,其中有兩個項目。這兩個項目都應該處理一些鍵。StackView中的多項關鍵處理
我會假設,如果StackView中的currentItem
不處理密鑰,密鑰將被轉發到較低層,但顯然情況並非如此。
以下示例說明了此問題。當按例如'A'時,我看到鑰匙由和堆棧視圖本身處理,但鑰匙不由layer0
處理。
注意layer0
由於transitionFinished
import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
id: mainWindow
visible: true
width: 1280
height: 720
color: "black"
Component {
id: layer0
Rectangle {
focus:true
width:200;height:200;color:"red"
Keys.onPressed: console.log("layer0")
}
}
Component {
id: layer1
Rectangle {
focus:true
width:200;height:200;color:"#8000FF00"
Keys.onPressed: console.log("layer1")
}
}
StackView {
id: stack
width: parent.width
height: parent.height
focus: true
Component.onCompleted: {
stack.push(layer0)
stack.push(layer1).focus=true
}
Keys.onPressed: {
console.log("StackView.onPressed")
}
delegate: StackViewDelegate {
function transitionFinished(properties)
{
properties.exitItem.visible = true
properties.exitItem.focus = true
}
}
}
}
感謝您的解釋(特別是父母vs兄弟姐妹部分)! 您的建議有兩個缺點:我們無法阻止傳播(雖然我們可以檢查event.accepted標誌),但我們不能保證(我認爲)密鑰最先被最高層接收。 我將看看Keys.forwardTo(lowerLayerItem)方法。 –