Qml:从文件加载组件时将参数作为URL的一部分传递

时间:2017-05-10 08:26:49

标签: c++ qt qml qtquick2

是否有机制在Qml中传递URL参数并在以后提取它们?:

StackView.push("Page.qml?#label2);

此致

4 个答案:

答案 0 :(得分:2)

从组件创建对象时,您可以“传递参数”。

StackView.push(component.createObject(null, {"someProperty" : someValue}))

因此,您可以使用辅助组件来促进:

Component { id: component; url: "Page.qml" } // or
property Component component: Qt.createComponent("Page.qml") // or
Component { id: component; Page {} }

或者如果你不想用额外的东西污染,你可以直接:

StackView.push(Qt.createComponent("Page.qml").createObject(null, {"someProperty" : someValue}))

最后,将StackView命名为StackView并不是一个好主意,我的意思是在QML中属性和ID都不能以大写字符开头。

答案 1 :(得分:1)

无需手动创建对象。就这样做

stackView.push(Qt.resolvedUrl("qrc:/U/R/L/item.qml"), {someCustomProperty: 0})

答案 2 :(得分:1)

这不起作用:

navigationBar.push(Qt.resolvedUrl('qrc:/Pages/BookListPage.qml'), {argument:'test'})

但这是有用的(创建一个对象):

navigationBar.push(Qt.createComponent("qrc:/Pages/BookListPage.qml").createObject(null, {argument:'test'}))

答案 3 :(得分:0)

实际上,根据QT文档([http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html][1]),建议的方法是将至少包含以下最少条目的属性列表传递给push函数():

  • 项目:此属性为必填项,用于保存要推送的项目。
  • 属性:推送时要分配给该项目的QML属性列表。这些属性将在加载时复制到项目中 时间或项目何时成为当前项目(通常在 推)。

所以我们得到

    navigationBar.push({item: Qt.resolvedUrl("MyRectangle.qml"), properties: {"color" : "red"}});