October 31, 2010

coderbob coderbob
Lab Rat
6 posts

Global Object, Scope and javascript

 

main.qml

  1. import Qt 4.7
  2.  
  3. import "myscript.js" as MyScript
  4.  
  5. Rectangle {
  6.    id:main
  7.  
  8.     MyScript.myFuction(); // This will execute fine, scope is fine
  9.     SecondQML {
  10.    }
  11.  
  12. }

myscript.js

  1. function myFuction() {
  2.   print('inside my function');
  3. }

SecondQML.qml

  1. Rectangle {
  2.  
  3.  MouseArea {
  4.          id: mouseArea
  5.          anchors.fill: parent
  6.          onClicked: {
  7.               MyScript.myFunction(); // Will not execute
  8.          }
  9.      }
  10. }

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.

7 replies

October 31, 2010

2beers 2beers
Lab Rat
111 posts

try parent.MyScript.myFunction();

October 31, 2010

coderbob coderbob
Lab Rat
6 posts

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

October 31, 2010

2beers 2beers
Lab Rat
111 posts

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

October 31, 2010

coderbob coderbob
Lab Rat
6 posts

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.

November 1, 2010

mbrasser mbrasser
Lab Rat
452 posts

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
    1. function myFunction() {
    2.     MyScript.myFuction();
    3. }
  • Use the library pragma [doc.qt.nokia.com] to import a shared copy of the script to both files.

Regards,
Michael

November 15, 2010

oseeker oseeker
Lab Rat
1 posts

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.

November 16, 2010

mbrasser mbrasser
Lab Rat
452 posts
oseeker wrote:
If import is document specific, how can I use the same global variant in two different qml files, they don’t have any relationship.

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

 
  ‹‹ [Solved] accessing file:/// outside the .QRC      [Solved] ListView Custom snapMode ››

You must log in to post a reply. Not a member yet? Register here!