20

QmlApplication のメイン QML ウィンドウの子ウィンドウとして、まったく新しいウィンドウ インスタンスを作成する方法はありますか?

// ChildWindow.qml
Rectangle {
    id: childWindow
    width: 100
    height: 100
    // stuff
}

// main.qml
Rectangle {
    id: window
    width: 1000
    height: 600

    MouseArea {
        anchors.fill: parent
        onClicked: createAWindow(childWindow);
    }
}

Q_OBJECTnew 内で新しいウィンドウをインスタンス化するためだけにクラスを作成しないようにしていますQmlApplicationViewer

4

2 に答える 2

45

Qt.createComponent を使用してそれを行うことができます。例 (Qt 5.3 を使用):

main.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    id: root
    width: 200; height: 200
    visible: true

    Button {
        anchors.centerIn: parent
        text: qsTr("Click me")

        onClicked: {
            var component = Qt.createComponent("Child.qml")
            var window    = component.createObject(root)
            window.show()
        }
    }
}

Child.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    id: root
    width: 100; height: 100

    Text {
        anchors.centerIn: parent
        text: qsTr("Hello World.")
    }
}
于 2014-06-25T12:24:34.423 に答える
2

組み込みの QML 機能のみを使用してトップレベル ウィンドウを作成する方法はありません。

ただし、 Qt Labs にはDesktop Componentsと呼ばれるプロジェクトがあり、特にWindow componentが含まれており、新しいトップレベル ウィンドウを作成できます。

于 2011-11-30T15:18:47.900 に答える