问题
In my Qt Android app I push an Item from the menu to the StackView:
import QtQuick 2.0
import QtQuick.Controls 1.0
ApplicationWindow
{
menuBar: MenuBar
{
Menu
{
title: qsTr("Menu")
MenuItem
{
text: qsTr("Push page")
onTriggered: stackView.push("qrc:/qml/SecondPage.qml")
}
}
}
StackView
{
id: stackView
anchors.fill: parent
// Implements back key navigation
focus: true
initialItem: FirstPage
{
width: parent.width
height: parent.height
}
}
}
SecondPage.qml:
import QtQuick 2.0
import QtQuick.Controls 1.0
Item
{
width: parent.width
height: parent.height
// focus: true
Keys.onBackPressed: stackView.pop()
Label
{
anchors.centerIn: parent
text: "Some text"
font.pointSize: 32
color: "gray"
}
}
Even though, back key exits the whole app. I tried adding focus: true
to where it is commented out and calling event.accepted = true
in Keys.onBackPressed
. Why is the back key press ignored?
回答1:
I should've put the pop
statement into the StackView
itself, not into a child page:
StackView
{
id: stackView
anchors.fill: parent
// Implements back key navigation
focus: true
initialItem: FirstPage
{
width: parent.width
height: parent.height
}
Keys.onBackPressed: pop()
}
来源:https://stackoverflow.com/questions/32092532/back-key-navigation-doesnt-work-even-though-focus-is-set-to-true