如何将一个 QQuickItem 扩展作为子扩展添加到另一个 QQuickItem 扩展?

How to add a QQuickItem extension as a child to another QQuickItem extension?

本文关键字:扩展 QQuickItem 添加 另一个 一个      更新时间:2023-10-16

我目前正在为Qt Quick建立一个C++工厂。我需要能够将工厂生成的子项添加到另一个自定义 QQuickItem 中,如下所示:

class Bar : public QQuickItem {
    Q_Object
    Bar(QQuickItem * parent = 0) : QQuickItem(parent) {
        // Generate some config called barConfig
        QQuickItem * newChild = FooFactory(barConfig);
        // Add child here?
    }
}

虽然实际上,有一个管理工厂配置的BarModel,但这在这里似乎无关紧要。那么,如何将我的newChild添加为Bar实例的子实例呢?

使用 setParentItem()

Bar(QQuickItem * parent = 0) : QQuickItem(parent) {
    // Generate some config called barConfig
    QQuickItem * newChild = FooFactory(barConfig);
    newChild->setParentItem(this);
}