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. Global Object, Scope and javascript

Global Object, Scope and javascript

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 5.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.
  • C Offline
    C Offline
    coderbob
    wrote on last edited by
    #1

    main.qml
    @
    import Qt 4.7

    import "myscript.js" as MyScript

    Rectangle {
    id:main

    MyScript.myFuction(); // This will execute fine, scope is fine
    SecondQML {
    

    }

    }
    @

    myscript.js
    @
    function myFuction() {
    print('inside my function');
    }
    @

    SecondQML.qml
    @
    Rectangle {

    MouseArea {
    id: mouseArea
    anchors.fill: parent
    onClicked: {
    MyScript.myFunction(); // Will not execute
    }
    }
    }
    @

    I also tried main.MyScript.myFunction() it is out of scope. I need to be able to reference the same script instance bound in main.qml and not a newly initiated script from inside secondQML.qml

    I can access main from here because of hierarchy but I am not sure why I cannot get to the MyScript property.

    1 Reply Last reply
    0
    • 2 Offline
      2 Offline
      2beers
      wrote on last edited by
      #2

      try parent.MyScript.myFunction();

      1 Reply Last reply
      0
      • C Offline
        C Offline
        coderbob
        wrote on last edited by
        #3

        TypeError: Result of expression 'parent.MyScript' [undefined] is not an object.

        1 Reply Last reply
        0
        • 2 Offline
          2 Offline
          2beers
          wrote on last edited by
          #4

          try parent.parent.MyScript.myFunction(); because the first parent is the rectangle from SecondQML and the parent to that is main rectangle

          1 Reply Last reply
          0
          • C Offline
            C Offline
            coderbob
            wrote on last edited by
            #5

            I don't believe it to be a parenting issue, as I have tried was you have suggested and made sure it was the proper parent. I think it has to do with the way the property MyScript is declared as an import. It might need to be referenced in a different manner or maybe it is not possible what I wish to do.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              Hi,

              Imports are document specific (we should try to make this clearer in the docs!), which is why the above is not working. A couple things you could try:

              • Add a "wrapper" in main.qml; this wrapper should be accessible to children
                @function myFunction() {
                MyScript.myFuction();
                }@
              • Use the "library pragma":http://doc.qt.nokia.com/4.7/qdeclarativejavascript.html#stateless-javascript-libraries to import a shared copy of the script to both files.

              Regards,
              Michael

              1 Reply Last reply
              0
              • O Offline
                O Offline
                oseeker
                wrote on last edited by
                #7

                Hi
                If import is document specific, how can I use the same global variant in two different qml files, they don't have any relationship.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mbrasser
                  wrote on last edited by
                  #8

                  [quote author="oseeker" date="1289825785"]If import is document specific, how can I use the same global variant in two different qml files, they don't have any relationship.[/quote]

                  In most cases there will be a shared "root" QML file (the root of the QML tree for your application), which is accessible throughout the application (if the root component has id "root", its properties will be available to all ancestors as root.propertyName).

                  For a shared variable in a JS file, the "library pragma" solution mentioned above should allow access to the same value from different QML files (both QML files will need to import the JS file).

                  1 Reply Last reply
                  0

                  • Login

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