Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] How to call Parent QML Screen's JavaScript function

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

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    at_pradeep
    wrote on 10 Oct 2011, 12:39 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tmcguire
      wrote on 10 Oct 2011, 13:37 last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        at_pradeep
        wrote on 11 Oct 2011, 06:10 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          task_struct
          wrote on 11 Oct 2011, 07:17 last edited by
          #4

          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.

          "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

          • Linu...
          1 Reply Last reply
          0
          • T Offline
            T Offline
            tmcguire
            wrote on 11 Oct 2011, 07:35 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              at_pradeep
              wrote on 11 Oct 2011, 12:21 last edited by
              #6

              Thanks for the explanation.

              1 Reply Last reply
              0

              4/6

              11 Oct 2011, 07:17

              • Login

              • Login or register to search.
              4 out of 6
              • First post
                4/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved