在我的 Qt Android 应用程序中,我将一个项目从菜单推送到 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
}
}
}
第二页.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"
}
}
尽管如此,返回键会退出整个应用程序。我尝试将 focus: true
添加到它被注释掉的位置,并在 Keys.onBackPressed
中调用 event.accepted = true
。为什么后退键被忽略了?
最佳答案
我应该将 pop
语句放入 StackView
本身,而不是子页面:
StackView
{
id: stackView
anchors.fill: parent
// Implements back key navigation
focus: true
initialItem: FirstPage
{
width: parent.width
height: parent.height
}
Keys.onBackPressed: pop()
}
关于android - 即使焦点设置为 true,后退键导航也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092532/