21

Is there a way to create a completely new window instance, as a child window of the main QML window in a QmlApplication?

// 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);
    }
}

I am trying to avoid writing a Q_OBJECT class just for instanciating the new window within a new QmlApplicationViewer.

2 Answers 2

47

You can do it by using Qt.createComponent. Example (using 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.")
    }
}
8
  • 1
    Inside main i am getting this error "qrc:///main.qml:59: ReferenceError: root is not defined", when i press click me button ?
    – User
    Aug 7, 2014 at 2:27
  • 2
    @User: "root is not defined" sounds like you forgot to ad the id: root to the ApplicationWindow in the main.qml-file... Also why not try importing the newer QtQuick.Controls? Most likely this will not break anything but improve small things...
    – mozzbozz
    Feb 4, 2015 at 13:29
  • 3
    The Error "root not defined" is because its unquoted. this way should work: var window = component.createObject("root") Jun 19, 2019 at 23:41
  • 1
    @MohammadKanan that is absolutely wrong. Please read the documentation on frameworks you use rather than guessing what might be required. The parameter passed to createObject is the parent of the created component. You can omit that but then you have to keep a reference to the object or it will be destroyed. Passing a string there doesn't make any sense. Sep 13, 2020 at 17:07
  • 1
    No, you stated that the ID should be quoted and that is incorrect. The method expects a QtObject as parameter and AFAIK a string is not a QtObject. Hence, this is undefined behavior. I assume it works because it ignores your invalid parameter and just assumes no parent but I haven't looked at the relevant code so that's just a guess. Sep 15, 2020 at 6:35
2

There is no way to create top-level windows using only built-in QML functionality.

However there's a project on Qt Labs called Desktop Components, which among other things contains a Window component, which allows you to create new top-level windows.

1
  • That looks awesome, thank you. My next step would have been to search for these Desktop Components, since I need them anyways. Didn't think they would do new windows, too ;)
    – opatut
    Nov 30, 2011 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.