如何从C++扩展的QQuickItem创建QQuickWindow作为子窗口?

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

本文关键字:QQuickWindow 窗口 创建 QQuickItem C++ 扩展      更新时间:2023-10-16

这个问题的动机是以下QML:

ApplicationWindow {
Rectangle {
Text { text: "Hello World" }
}
Item {
// I do something
Window {
Text { text: "Hello world too!" }
}
}
}

在此示例中,有一个应用程序窗口,然后在项内有第二个窗口。我正在尝试复制这种用法,但是通过在扩展QQuickItem中实例化QQuickWindow,但根据文档,我不能QQuickItem不属于QWindow类型。我想要的是这个:

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

不是项目和任何元素的子元素,这些元素可以通过以下代码轻松查看:

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的孩子,另一方面Window没有父母。