Calling another window from main window



  • Hi, I looked in many examples including Qt examples but i couldn't find any example that calls another window from the main window with menu, toolbar, etc. I made minimal app but I'm in dilemma of what is the right approach. I have 2 qml files named mainform.qml and subform.qml both ApplicationWindow types. I created QQmlApplicationEngine i loaded both files with QQmlApplicationEngine::load() method. Now, if this is the right way (I succeded with this approach ) do I have to pass around pointer to only instance of QQmlApplicationEngine or do I have to declare multiple engines when I need subform (which didn't work for me , every form instantiated after first one was non responsive, you couldn't click buttons, tabview tabstack wasn't there, etc.) or as in qt quick control text editor example I call it from qml file with mySubForm.show(). I suppose this is extremely trivial and beginner question but give me headache for hours. Also is there any non trivial open source qt quick app to learn by reading the code. Thanks in advance.



  • I've done something where I can pass in a custom qml file to load that can include Window.
    @ Loader {
    id: scriptLoader

                property var model
                signal finished
    
                anchors.fill: engine
                visible: false
    
                onFinished: {
                    project.scriptDone()
                    source = ""
                }
    
                Connections {
                    target: project
                    onScriptInvoked: {
                        scriptLoader.model = state
                        scriptLoader.source = script
                    }
                }
    
                // Make sure windows get closed
                Connections {
                    target: window
                    onClosing: {
                        if(scriptLoader.item) {
                            scriptLoader.item.visible = false;
                        }
                    }
                }
            }@
    

    Then for an example qml file
    @import QtQuick 2.1
    import QtQuick.Window 2.1
    import QtQuick.Controls 1.0

    Window {
    visible: true
    flags: Qt.Dialog

    onClosing: finished()
    
    property var kv: model.keyValue("string")
    Column {
        Text {
            text: kv.name + " " + kv.value 
        }
    
        Button {
            text: "Helloooo"
            onClicked: console.debug("Button clicked")
        }
    }
    

    }
    @

    Couple of points to note

    • I had problems getting the window to close when the application closed hence the bit of code in Connections
    • All the model and kv stuff in there you can ignore. I just copied exactly what I've got.

    Also for examples have you found the qtquickcontrols/examples folder? There are a few other scattered examples folders as well that I've found quite helpful.



  • Thank you Babalas qml approach is my slimmer side but this is perfect chance to learn more :).Thanks again.



  • Glad to help. I've stumbled my way through so I'm not certain if it is the "best" way to do it but it works.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.