1

Looking at the question How can I create a new window from within QML? we see there that we can create a new window this way:

main.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

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

    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.")
    }
}

But this way doesn't seem possible to pass variables to the child.qml and then after we submit a form on this new Window get them on the main.qml, or is it possible? How can we do it?

About the variables being transfered to Child.qml i suppose something like this will do:

                var b = tabButton.createObject(tabbar, {tabId: tabId, trCtx: tabs[t], tabTitle: c.title,
                                                        "font.pixelSize": 14, "font.bold": globals.menu.fontBold,
                                                        "font.family": robotoRegular.name})

If this is okey, then the only thing missing is getting values from Child.qml.

Ngranja1512
  • 111
  • 1
  • 9

1 Answers1

1

To initialize a new object you can either use the initialization list or assign the new variables after object creation. Using the initialization list is of course the best solution. If you really want to pass "variables" (like a list or something like that) your main window can always call a function of the child window.

The communication between the two windows can be archieved by using signals and slots. You can either connect with JavaScript or with QML. In the example you will find both ways. You can than listen to your own signals or listen to property changes.

main.qml

//========================
//== Includes

import QtQuick 2.3
import QtQuick.Controls 1.2

//========================
//== Base Item

ApplicationWindow {

    //========================
    //== Properties

    property var childWindowHandle: null

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

    //========================
    //== Childs

    Button {

        //========================
        //== Properties

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

        //========================
        //== Functions

        function outputNewValue(){
            console.log(childWindowHandle.iWillChangeSoon)
        }

        //========================
        //== Connections

        onClicked: {
            var component = Qt.createComponent("Child.qml")

            // Using the "constructor" to initialize the object WHILE creating (no change signals emitted)
            root.childWindowHandle = component.createObject(root, { withConstructor: "\\(^_^)/" })

            // Check if creation was successfully
            if(root.childWindowHandle){

                // Change the value AFTER construction. The onAfterConstructorChanged signal get fired
                root.childWindowHandle.afterConstructor = "_(._.)_";

                // "Listen" on variable change. Every property has a corresponding change signal ("Changed" after name)
                root.childWindowHandle.iWillChangeSoonChanged.connect(outputNewValue)

                // Just show window...
                root.childWindowHandle.show()
            }
        }
    }

    Connections{ // <- You can also use "Connections" to listen for changes

        //========================
        //== Properties

        target: root.childWindowHandle ? root.childWindowHandle : null
        ignoreUnknownSignals: true // Important, because "childWindowHandle" can be "null"

        //========================
        //== Connections

        onSpecialSignal: console.log("An other signal fired!")
    }
}

Child.qml

//========================
//== Includes

import QtQuick 2.3
import QtQuick.Controls 1.2

//========================
//== Base Item

ApplicationWindow {

    //========================
    //== Properties

    property string withConstructor: ""
    property string afterConstructor: ""
    property string iWillChangeSoon: ""

    id: root
    width: 100;
    height: 100

    //========================
    //== Signals

    signal specialSignal()

    //========================
    //== Connections

    Component.onCompleted: {
        console.log("Child Component Completed")
        console.log("withConstructor:" + withConstructor)
        console.log("afterConstructor:" + afterConstructor)
    }

    //========================
    //== Childs

    Text {

        //========================
        //== Properties

        anchors.centerIn: parent
        text: qsTr("Hello World.")
    }

    Timer{

        //========================
        //== Properties

        running: true
        interval: 5000

        //========================
        //== Connections

        onTriggered:{

            // Change variable
            root.iWillChangeSoon = "Yep, I changed ;)"

            // Emit our special signal
            root.specialSignal();
        }
    }
}
t4ggno
  • 131
  • 6