我正在尝试创建一个 QML 对象,它就像其他对象的包装器一样。这是我的 QML 文件(Container.qml):
Item {
property string label
property Item control
Row {
Label {
text: label
}
// Not sure how to display the control assigned to the control property
}
}
我想做的(在我使用这个组件的 QML 中)是这样的:
Container {
label: "My Label"
control: Textbox {
text: "My Value"
}
}
当输入该 QML 时,结果(在界面中)应该类似于此 QML 的输出:
Item {
Row {
Label {
text: "My Label"
}
Textbox {
text: "My Value"
}
}
}
这可能吗?当我尝试这样做时,在将项目分配给控件属性时,我得到“无法将对象分配给属性”。我搜索了 Qt 论坛并无情地用谷歌搜索,但没有成功。如果有人知道答案,将不胜感激。
谢谢
jack
最佳答案
有更好的解决方案:
/* MyObject.qml */
Rectangle {
default property alias data /* name can be any */ : inner_space.data
/* ... You can put other elements here ... */
Item {
id: inner_space
/* ... Params ... */
}
/* ... You can put other elements here ... */
}
现在我们可以做我们想做的一切了!
/* main.qml */
Rectangle {
MyObject {
Button {
/* ... */
}
}
}
感谢用户bobbaluba建议使用
data
属性而不是 children
.
关于properties - 如何将 QML 项目分配给 QML 中的组件属性,然后在组件内使用该对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021350/