在我的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
}
}
}
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"
}
}
即使后退键退出整个应用程序。我尝试将focus: true
添加到注释掉的位置并在event.accepted = true
中调用Keys.onBackPressed
。为什么忽略后退按键?
答案 0 :(得分:2)
我应该将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()
}