我有一个QMLLoader
加载另一个 qml
Loader { id: gaugeLoader }
PieMenu {
id: pieMenu
MenuItem {
text: "Add Bar Gauge"
onTriggered: gaugeLoader.source = "qrc:/Gauges/horizontalBarGauge.qml"
}
MenuItem {
text: "Action 2"
onTriggered: print("Action 2")
}
MenuItem {
text: "Action 3"
onTriggered: print("Action 3")
}
}
我如何传递参数来设置ID
, width
, height
加载的qml等等?
方法一: Loader::setSource
您可以使用Loader::setSource(url source, object properties) http://doc.qt.io/qt-5/qml-qtquick-loader.html#setSource-method函数在构造过程中设置属性,例如:
gaugeLoader.setSource("qrc:/Gauges/horizontalBarGauge.qml", {"width": 100, "height": 100});
请注意,您无法设置id属性 http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#the-id-attribute这样,因为它不是一个普通的 property 属性:
一旦创建了对象实例,其 id 属性的值
无法更改。虽然它可能看起来像一个普通的属性,但 id
attribute不是普通的property属性,还有特殊的语义
适用于它;例如,无法访问 myTextInput.id
在上面的例子中。
相反,您可以创建属性别名,如下所示:
property alias gauge: gaugeLoader.item
方法二:相对于 Loader 对象的几何形状
作为替代方案,您可以设置width
and height
on the Loader
对象并指定宽度和高度horizontalBarGauge.qml
相对于其父级,即Loader
object.
property alias gauge: gaugeLoader.item
Loader {
id: gaugeLoader
width: 100
height: 100
}
qrc:/Gauges/horizontalBarGauge.qml:
Item {
anchors.fill: parent
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)