Austin
Austin

Reputation: 1020

Spawn QtQuick UI file from a normal QML Window class

How does one display a QtQuick UI fileset (ui.qml and .qml) from a normal QML Window?

To display other QML windows from my parent Window class, the call is, roughly:

                var component = Qt.createComponent("SubWindow.qml")
                var window    = component.createObject(window)
                window.show()

I've tried calling both the QtQuickUISubwindow.ui.qml and QtQuickUISubwindow.qml, but neither works.

Are QtQuickUI files not meant to be sub windows?

Upvotes: 0

Views: 354

Answers (1)

derM
derM

Reputation: 13691

Window and ApplicationWindow are not of Type Item (or QQuickItem). This however is a requirement for being placed as a root item in a .ui.qml-file as stated in the documentation:

The following features are not supported:

  • [...]
  • Root items that are not derived from QQuickItem or Item

So the answer is:

No, you can't use QtQuickUI files neither as windows nor as sub windows.


You can however easily use them within a sub window

// main.qml

Window {
    id: root
    width: 800
    height: 600
    visible: true
    Loader { // creates my sub window without JS
        id: winLoader
        active: false // Change to create the window.
        sourceComponent: Window {
            width: srcLoader.item ? srcLoader.item.width : 0
            height: srcLoader.item ? srcLoader.item.height : 0
            visible: srcLoader.item
            Loader {
                id: srcLoader
                source: "QtQuickUISubwindow.ui.qml" // Your ui.qml-file *here*
                active: true
            }
            onClosing: winLoader.active = false
        }
    }
    Button {
        text: "Show sub window!"
        onClicked: winLoader.active = true
    }
}

This code has not been tested by me. Maybe I'll do so later once I have access to a Qt machine. How to initialize multiple windows with a repeater and a ListModel you can find here: https://stackoverflow.com/a/47018205/2056452

You might pass the source of the srcLoader to the model, and read it from there if you want to open multiple different windows.


You can ofc modify the QtQuickUISubwindow.qml-file and add a Window or ApplicationWindow of the appropriate size as the root element. Then you can create everything as you used to do, using JS - and hope that the garbage collector plays nicely.

Upvotes: 1

Related Questions