如何创建对话框并动态设置标题和文本
查看Ubuntu SDK文档中的Dialog组件示例 ,看起来好像将对话框定义为具有固定标题和文本的静态组件。 或者至少在显示对话框之前我无法弄清楚如何更改它。
我也被暗示了Dialog所基于的PopupBase类的show()方法 ,但我还没有弄清楚如何将它们用于我的目的。
我的代码中有一个信号处理程序,我想打开一个对话框并动态设置标题和文本。
onSomethingHappened: { /* Open a dialog and set the title and text properties */ }
我怎样才能做到这一点?
这不是您问题的答案,因为对话框文本没有直接更改,但它可能是您的问题的答案,因为对话框文本动态更改:-)
假设你有一些触发onSomethingHappened
,你可以将对话框的属性连接到项目的属性。
例:
Item { Component { id: dialog Dialog { id: dialogue title: someID.dialogTitle text: someID.dialogText Button { text: "cancel" onClicked: PopupUtils.close(dialogue) } } } } SomeItem { id: someID property string dialogTitle property string dialogText onSomethingHappened: { dialogTitle = "Hello David" dialogText = "Whats up?" PopupUtils.open(dialog) } }
我发现我可以在我的代码中使用以下代码段执行此操作( root
是open()
方法的调用者ID,但在此示例中可以忽略)。 基本上,在PopUtils.open()函数中填充params
参数:
PopupUtils.open(Qt.resolvedUrl("QrCodeDialog.qml"), root, { title: i18n.tr("This is the title"), text: i18n.tr("This is the text") })
然后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) } }