[Solved] How to call Parent QML Screen's JavaScript function



  • I situation is as follows:

    I have MainScreen.qml with MainScrene.js included in it and
    I have PopupScreen.qml with PopupScreen.js included in it.

    I am opening PopupScreen.qml from the main screen qml file using,

    @var component = Qt.createComponent("PopupScreen.qml");
    var sprite = component.createObject(parentWindow, {"x":120, "y":130});@

    Now when user clicks the button on the popup window I need to call one of the javascript function which is in MainScrene.js from the PopupScreen.js javascript.

    So can i directly include MainScrene.js in the PopupScreen.js and call the javascript function?
    Including MainScrene.js in PopupScreen.js does it maintains the state/value of all the global variables defined in the MainScrene.js file or it includes a fresh copy of the MainScrene.js in PopupScreen.js.

    Thanks.



  • In PopupScreen.qml, emit a signal, and react on it in Mainscreen.qml.



  • can you provide a example on how to do it... i am in badly need of this.
    How to emit signals from qml, how to connect them and how to handle them with the qml?
    Can you please provide one example for this.

    thanks for the reply.



  • QML signals are explained "here":http://doc.qt.nokia.com/4.7-snapshot/qmlevents.html

    To connect signals to (or receive signals from) dynamically created objects, use the signal connect() method. See "Connecting Signals to Methods and Signals":http://doc.qt.nokia.com/4.7-snapshot/qmlevents.html#connecting-signals-to-methods-and-signals for more information.



  • Have a look at "the documentation":http://doc.qt.nokia.com/latest/qml-extending-types.html#adding-signals , it tells you how to define and emit signals. Read that carefully.
    I would do something like this:

    main.qml:
    @
    import QtQuick 1.0

    Rectangle {
    id: root
    width: 500
    height: 500
    color: "green"

    property Item __dialog;

    Connections {
    target: __dialog
    onClicked: console.log("Popup clicked.")
    }

    MouseArea {
    anchors.fill: parent
    onClicked: {
    var component = Qt.createComponent("PopupScreen.qml");
    __dialog = component.createObject(root, {
    "x":100,
    "y":100,
    });
    }
    }
    }
    @

    PopupScreen.qml:
    @
    import QtQuick 1.0

    Rectangle {
    id: root
    signal clicked

    width: 300
    height: 300
    color: "red"
    MouseArea {
    anchors.fill: parent
    onClicked: {
    root.clicked()
    }
    }
    }
    @

    In main.qml, I couldn't get the signal handler to be inline of the createObject() call, therefore I used the Connections element. Maybe I was just not using the right syntax.
    Anyway, the above works.



  • Thanks for the explanation.


Log in to reply
 

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