在QML中使用对话框

3

如果在某个时刻需要通过调用对话框或类似的方式获取用户输入,使用QML实现的最佳方式是什么?是否有js中prompt的类似物?


1
你的目标是台式电脑、手机还是iPad类型设备? - Nathaniel Johnson
1
这是一个相当广泛的问题,但我认为使用Loader之一的方法会起作用。http://qt-project.org/wiki/QML-Application-Structuring-Approaches - Nathaniel Johnson
1个回答

3
你可以使用自 Qt 5.3 起可用的 Dialog,并按照你的意愿进行自定义。 确保你已经安装并且运行了这个版本。 在这里 你可以找到一个例子。
此外,看一下我准备的一个小例子:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.2

ApplicationWindow {
    visible: true
    width: 320
    height: 240
    title: qsTr("Custom Dialog")

    Dialog {
        id: customDialog
        title: "Custom Dialog in QML/Qt 5.3"
        standardButtons: StandardButton.Ok | StandardButton.Cancel

        Column {
            anchors.fill: parent
            Text {
                text: "Here goes all your custom elements..."
            }
            TextInput {
                id: edtInput
                text: "Input text"
            }
        }

        onButtonClicked: {
            if (clickedButton==StandardButton.Ok) {
                console.log("Accepted " + clickedButton)
                lblResults.text += edtInput.text
            } else {
                console.log("Rejected" + clickedButton)
            }
        }
    }
    Column {
        anchors.fill: parent

        Button {
            text: qsTr("Call Custom dialog")
            onClicked: customDialog.open()
        }

        Text {
            id: lblResults
            text: qsTr("Results: ")
        }
    }
}

实际上,对话框组件甚至在5.3之前就已经存在了,尽管处于实验状态。请参见顶部的行“自:Qt 5.2”。 - László Papp
1
示例链接已失效。https://stuff.mit.edu/afs/athena/software/texmaker_v5.0.2/qt57/doc/qtquickdialogs/qml-qtquick-dialogs-dialog.html是有效的。 - Pedro Luz
1
@PΞDROLUZ 谢谢您指出这个问题。链接已经更新。 - tro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接