我有一个QML Loader
,它会加载另一个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")
}
}
如何传递参数来设置加载的qml的ID
,width
,height
等等?
答案 0 :(得分:6)
方法1: Loader::setSource
您可以使用Loader::setSource(url source, object properties)
功能在构造期间设置属性,例如:
gaugeLoader.setSource("qrc:/Gauges/horizontalBarGauge.qml", {"width": 100, "height": 100});
请注意,您无法以这种方式设置id
attribute,因为它不是普通的属性属性:
创建对象实例后,其id属性的值 无法改变。虽然它可能看起来像普通的财产,身份证 attribute不是普通的属性属性,而是特殊的语义 适用于它;例如,无法访问myTextInput.id 在上面的例子中。
相反,您可以按如下方式创建属性别名:
property alias gauge: gaugeLoader.item
方法2:相对于Loader对象的几何
作为替代方案,您可以在width
对象上设置height
和Loader
,并指定horizontalBarGauge.qml
相对于其父级的宽度和高度,即{ {1}}对象。
Loader
QRC:/Gauges/horizontalBarGauge.qml:
property alias gauge: gaugeLoader.item
Loader {
id: gaugeLoader
width: 100
height: 100
}
答案 1 :(得分:1)
如果没有@ m7913d,我将永远无法弄清楚。我正在扩展他的答案(使用选项2 )来帮助其他尝试做与我类似的事情的人:
注意:我正在努力了解加载程序正在接收model::data(..)
返回的UserData。
使用加载程序传递model::data(.....)
返回的UserData
GridView {
id: trainingDataSelectGrid
anchors.fill: parent
cellWidth: parent.width/3
cellHeight: parent.height/2
model: trainingDataListModel
delegate: Component {
id: trainingDataDelegate
Loader {
sourceComponent: {
if (index == 0) {
return trainingDataModel.addNewComp
} else {
return trainingDataModel.dataComp
}
}
// Loader binds properties to the source component
// passed to the view delegate
property string _name: name
property string _author: author
property string _author_email: author_email
property string _created: created
property string _updated: updated
property int _net: net
property int _img_width: img_width
property int _img_height: img_height
property int _index: index
}
}
TrainingDataModel {
id: trainingDataModel
cardWidth: trainingDataSelectGrid.cellWidth
cardHeight: trainingDataSelectGrid.cellHeight
}
}
我的两个源组件都存在于trainingDataModel
中(QML:TrainingDataModel
)。
之前:
property Component dataComp: activeCard
property Component addNewComp: addNew
property string name
property string author
property string author_email
property string created
property string updated
property int net
property int img_width
property int img_height
property int index
property int cardWidth
property int cardHeight
property int paneWidth: cardWidth * 0.95
property int paneHeight: cardHeight * 0.95
property int materialElevate: 5
property string materialBgColor: "white"
id: cardBody
Component {
id: addNew
Rectangle {
id: card
}
}
Component {
id: activeCard
Rectangle{......}
}
}
之后:将变量移动到Loader将实例化的组件的作用域
property Component dataComp: activeCard
property Component addNewComp: addNew
property int cardWidth
property int cardHeight
property int paneWidth: cardWidth * 0.95
property int paneHeight: cardHeight * 0.95
property int materialElevate: 5
property string materialBgColor: "white"
id: cardBody
Component {
id: addNew
}
Component {
id: activeCard
Rectangle {
id: activeCardBackground
width: cardWidth
height: cardHeight
color: "transparent"
property string name: _name
property string author: _author
property string author_email: _author_email
property string created: _created
property string updated: _updated
property int net: _net
property int img_width: _img_width
property int img_height: _img_height
property int index: _index
/// Use properties below
}
}