如何从 C++ 扩展 QQuickItem 创建 QQuickWindow 作为 child?

How to create QQuickWindow as a child from a C++ extended QQuickItem?

这个问题是由以下 QML 提出的:

ApplicationWindow {
    Rectangle {
        Text { text: "Hello World" }
    }

    Item {
        // I do something

        Window {
            Text { text: "Hello world too!" }
        }
    }
}

在此示例中,有一个应用程序 window,然后在一个项目中有第二个 window。我试图复制这种用法,但是通过在扩展的 QQuickItem 中实例化 QQuickWindow,但是根据文档我不能,因为 QQuickItem 不是 [=16] 类型=].我想要的是:

class Foo : public QQuickItem {
private:
    QQuickWindow * childWindow;
public:
    Foo(QQuickItem * parent = 0) : QQuickItem(parent) {
        childWindow = new QQuickWindow();
        childWindow->setParent(this);
        // Add custom items to childWindow
    }
}

不幸的是,这在 childWindow->setParent(this) 再次失败,因为 QQuickItem 没有扩展 QWindow。我怎样才能以类似的方式做到这一点?

Window不是item的child,也不是任何元素的child,用下面的代码很容易看出:

ApplicationWindow {
    width: 100
    height: 100
    visible: true
    Rectangle {
        Text { text: "Hello World" }
    }

    Item{
        id: item
        Window{
            id: new_window
            visible: true
            color: "red"
            Component.onCompleted: console.log("new_window :",new_window.parent)
        }
        Component.onCompleted: console.log("item :", item.parent)
    }
}

输出:

qml: item : ContentItem_QMLTYPE_10(0x56353791dbe0)
qml: new_window : undefined

很明显,ItemcontentItem的child , 另一方面 Window 没有 parent.