Results 1 to 2 of 2

Thread: Using global variables from QML and Javascript

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Using global variables from QML and Javascript

    Hi,

    In my program i have to access and change the value of a variable from different QML-files and from Javascript-files.
    I'm using a JavaScript file to store the value:
    Qt Code:
    1. //GlobalData.js
    2.  
    3. .pragma library
    4. var myValue = 42;
    To copy to clipboard, switch view to plain text mode 

    In my QML-files i use the variable like this:
    Qt Code:
    1. //example.qml
    2.  
    3. import "GlobalData.js" as GData
    4.  
    5. ...
    To copy to clipboard, switch view to plain text mode 

    Everything works fine. There is only one instance of the GlobalData.js file and every QML-document can read and write myValue.

    But now, i included GlobalData.js into a JavaScript like this:
    Qt Code:
    1. //myJavaScript.js
    2.  
    3. Qt.include("GlobalData.js");
    4. console.log(myValue);
    To copy to clipboard, switch view to plain text mode 

    The problem is, that myJavaScript.js seems to create its own instance of GlobalData.js, so that if I modify myValue in a QML-Document, it's not changing in myJavaScript.js.


    Is there any possibility to let myJavaScript access the same instance of GlobalData.js as the QML-Documents do, instead of creating its own one?


    Thank You for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using global variables from QML and Javascript

    Provide a function in a qml file that will modify that property and call that function from within this JavaScript file. Seems complicated but your design is probably a bit wrong anyway if you have to do something like that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QDevelop - global variables & help
    By impeteperry in forum Qt-based Software
    Replies: 2
    Last Post: 9th June 2011, 23:28
  2. Are global variables evil?
    By Wonk0 in forum Newbie
    Replies: 3
    Last Post: 2nd February 2011, 20:11
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.