我想在TabView/Tab中动态地向ColumnLayout添加一个组件。但我还没找到这么做的可能性。问题是,对于ColumnLayout调用,我没有正确的父引用。由于Tab动态加载组件,所以我将ColumnLayout封装在组件中。
在QML中,我可以解决以下问题:objForm.tabView.tabStatus.tabStatusLoader.colLayout,,但我不能使用它作为正确的父级。
它似乎不在范围之内。
TypeError: Cannot read property 'tabStatus' of undefinedObjForm.ui.qml
Item {
id: item1
width: 400
height: 400
property QtObject object
property Component compLayout
TabView {
id: tabView
anchors.fill: parent
Tab {
id: tabStatus
title: "status"
Loader {
id: tabStatusLoader
sourceComponent: compLayout
}
}
}
}ObjForm.qml
ObjectViewForm {
id: objForm
anchors.fill: parent
object: someObj
compLayout: Component {
id: layoutComp
ColumnLayout {
id: colLayout
spacing: 2
}
}
onObjectChanged: {
// Here i want to add the someLabel Component to the ColumnLayout
someLabel.createObject(*PARENT*)
}
Component {
id: someLabel
Row {
property string text
property string label
spacing: 5
Label {
text: parent.label
}
Label {
text: parent.text
}
}
}有没有人知道如何解决这个问题,或者提出一个更好的建议?
发布于 2018-03-01 14:06:31
好的,我自己找到了解决办法。相反,为了将ColumnLayout包含到组件中,我提取了它并创建了一个别名属性来发布它。这样就可以将对象添加到我的ColumnLayout中。但是ColumnLayout得到的是错误的父级(ObjForm)而不是组件。
组件不能是父组件,因为需要QQuickItem*而不是QObject*,而且除了'id‘之外,其他组件不能包含属性。因此,需要一个虚拟项目。
要重新创建ColumnLayout,项目需要一个Component.onCompleted函数,其中父函数将被设置。
ObjectViewForm {
id: objForm
anchors.fill: parent
object: someObj
property alias componentLayout: colLayout
compLayout: Component {
id: layoutComp
Item {
id: dummy
Component.onCompleted: {
colLayout.parent = dummy
}
}
}
ColumnLayout {
id: colLayout
spacing: 2
}https://stackoverflow.com/questions/49046235
复制相似问题