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

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

我目前正在为 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);
}