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

标签 c++ qt qml qtquick2

这个问题是由以下 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 的 child ,另一方面Window没有 parent 。

关于c++ - 如何从 C++ 扩展的 QQuickItem 创建 QQuickWindow 作为 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953510/

相关文章:

c++ - Qt XML 查看器? (像 Notepad++ )

c++ - GCC 规范文件 : how to get the installation path

c++ - 检查应用程序是否首次运行

c++ - 通过 qproperties 更改小部件的样式属性

javascript - QML Javascript "console.log"与 UTF-8

c++ - QtQuick : Change expand icon in TreeView

C++ 删除 wchar_t* 指向的内容

C++字符串到双重转换

c++ - 如何从另一个函数访问一个函数中的内容,在 C++ 中从第三个调用

Qt5.6:高DPI支持和OpenGL(OpenSceneGraph)