QT / QML,对话框打开后,后退按钮无法在Android上运行?

时间:2016-04-21 23:41:25

标签: android qt qml

我找不到任何解决方案。我有一个包含TextArea和Ok和关闭按钮的对话框。当我打开对话框时,然后关闭它。后退按钮无法在Android上运行。为什么?我的代码是:

 Dialog {


    id: messagereject



    Connections
    {
        target:process
        ignoreUnknownSignals: true
        onSuccessrejectwo: {

            var task=stackView.get(0).currenttask;
            task.color="red";
            task.enabled=false;
            rejectreasontxt.text="";
        }
    }




  contentItem:ColumnLayout {
         id:rejectlay
        Layout.preferredHeight: rejectheadertxt.height+rejectreasontxt.height+dp(30)

        Layout.preferredWidth: rejectheadertxt.width+dp(100)

        spacing: dp(10)
      .......
    ......

1 个答案:

答案 0 :(得分:1)

我今天遇到了同样的问题。在我的应用程序中,我想使用带有TextField的对话框让用户编辑某些项目的标题。

在QML中使用Dialog的问题似乎是即使在对话框关闭后,它也会保留键盘焦点,因此也会捕获后退按钮。

对我来说,解决方案是确保在对话框关闭后(即其visible属性设置为false),活动键盘焦点将被传回“主窗口”:

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: mainWindow

    width: 200
    height: 150
    visible: true

    FocusScope {
        id: rootItem

        anchors.fill: parent

        Label {
            id: label

            text: qsTr("Hello World")
            anchors.centerIn: parent
        }

        Button {
            text: qsTr("Edit")
            anchors {
                right: parent.right
                bottom: parent.bottom
                margins: 10
            }

            onClicked: {
                edit.text = label.text;
                dialog.open();
                // Let the TextField gain keyboard focus. This also
                // means the main window no longer gets keyboard input
                // so from here on input (including the back button)
                // is handled by the dialog:
                edit.forceActiveFocus();
            }
        }
    }

    Dialog {
        id: dialog

        standardButtons: StandardButton.Ok | StandardButton.Cancel

        onAccepted: label.text = edit.text
        onVisibleChanged: {
            if (!visible) {
                // Force some item in the main window (in our case the "root item")
                // to gain active focus:
                rootItem.forceActiveFocus();
            }
        }

        TextField {
            id: edit
        }
    }
}