如何创建对话框并动态设置标题和文本

Dav*_*lla 5 application-development qml ubuntu-sdk ubuntu-touch

查看Ubuntu SDK 文档中的Dialog 组件示例,看起来好像对话框旨在被定义为具有固定标题和文本的静态组件。或者至少在显示对话框之前我无法弄清楚如何更改它。

我也被暗示过Dialog 所基于的 PopupBase 类show() 方法,但我还没有弄清楚如何将它们用于我的目的。

我的代码中有一个信号处理程序,我想在其中打开一个对话框并动态设置标题和文本。

onSomethingHappened: {
   /* Open a dialog and set the title and text properties */
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Dav*_*lla 4

我发现我可以使用代码中的以下代码片段来执行此操作(root是该方法的调用者 ID open(),但在本示例中可以忽略)。本质上,填充PopUtils.open()params函数中的参数:

PopupUtils.open(Qt.resolvedUrl("QrCodeDialog.qml"), root, {
                    title: i18n.tr("This is the title"),
                    text: i18n.tr("This is the text")
                })
Run Code Online (Sandbox Code Playgroud)

然后该QrCodeDialog.qml文件包含:

import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.Popups 0.1

Dialog {
    id: qrcodedialog
    title: ""
    text: ""

    Button {
        text: i18n.tr("Close")
        onClicked: PopupUtils.close(qrcodedialog)
    }
}
Run Code Online (Sandbox Code Playgroud)