Back key navigation doesn't work even though focus is set to true

吃可爱长大的小学妹 提交于 2019-12-24 16:15:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!