首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在组件中用createObject填充createObject

在组件中用createObject填充createObject
EN

Stack Overflow用户
提问于 2018-03-01 09:11:00
回答 1查看 438关注 0票数 1

我想在TabView/Tab中动态地向ColumnLayout添加一个组件。但我还没找到这么做的可能性。问题是,对于ColumnLayout调用,我没有正确的父引用。由于Tab动态加载组件,所以我将ColumnLayout封装在组件中。

在QML中,我可以解决以下问题:objForm.tabView.tabStatus.tabStatusLoader.colLayout,,但我不能使用它作为正确的父级。

它似乎不在范围之内。

代码语言:javascript
复制
TypeError: Cannot read property 'tabStatus' of undefined

ObjForm.ui.qml

代码语言:javascript
复制
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

代码语言:javascript
复制
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
        }
    }
}

有没有人知道如何解决这个问题,或者提出一个更好的建议?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-01 14:06:31

好的,我自己找到了解决办法。相反,为了将ColumnLayout包含到组件中,我提取了它并创建了一个别名属性来发布它。这样就可以将对象添加到我的ColumnLayout中。但是ColumnLayout得到的是错误的父级(ObjForm)而不是组件。

组件不能是父组件,因为需要QQuickItem*而不是QObject*,而且除了'id‘之外,其他组件不能包含属性。因此,需要一个虚拟项目。

要重新创建ColumnLayout,项目需要一个Component.onCompleted函数,其中父函数将被设置。

代码语言:javascript
复制
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
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49046235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档