×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    210

    Troubles setting js global variables from QML file

    Hello,
    I am building a type of construction calculator based on the concept of the QML "calculator" example included with the Qt SDK.
    In order to give the user some options for the calculations I created an options QML file that I would like to use to setup some variables in the js script file for the calculator.
    I implement the options menu as a State of the main calculator window. I am able to get the data from the menu state to set the js global variables no problem, but as soon as the js file is accessed from the main window the variables are reset to 0...
    Here is how I am attempting to do it:
    Code:
    // accuracy menu
    import QtQuick 1.0
    import "ccalc.js" as CalcE
        Rectangle {
            id: acmenu
            width: 300
            height: 300
            radius: 10
            color: "silver"
            anchors.fill: parent 
            function setOptions(decIndex, fracIndex) {CalcE.setup(decIndex, fracIndex)}
         //   Component.onCompleted: setOptions(lstDecAcc.currentIndex, lstFracAcc.currentIndex)
                   {...}
                        MouseArea{
                            id: mouseArea
                            height: parent.height; width: parent.width
                            onClicked: {
                                  setOptions(lstDecAcc.currentIndex, lstFracAcc.currentIndex)
                                window.state = ""
                            }
    That sets up the variables in the js file no problem. Here is the function in the js file that sets the global js variables:
    Code:
    //setup the globals 
    var _feet = 0, _inch = 0, _num = 0, _den = 0, _decPlace = 0 
              {...}
    function setup(decIndex, fracIndex){
        _decPlace = decIndex  + 1    //setup the dec place from options
        //check den from menu
    if(acmenu.fracIndex == "0"){
        _den = 8
    }else if(fracIndex == "1"){
        _den = 16
    }else if(fracIndex == "2"){
        _den = 32
    }else if(fracIndex == "3"){
        _den = 64
    }else if(fracIndex == "4"){
        _den = 1000
    }else if(fracIndex == "5"){
        //custom, den selected
        _den =  acmenu.dInput.valueOf() //set den based on input 1 to 10000
        console.log(_den)
    }
    
    console.log(_decPlace, _den)
    
    }
    That all works fine until the js file is accessed from the main window at which point the variables are reset to 0
    What am I doing wrong here? Perhaps I need to store the results from the menu as a settings file and then access the settings file from the js file directly?
    I looked at using the ".pragma library" option but that does not allow the js file to access the main QML file to set the display etc...

    Any advice for this rookie on the best way to achieve this would be greatly appreciated!

    Cheers,
    Jon

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Re: Troubles setting js global variables from QML file

    Hi Jon,

    JavaScript files imported into a QML file are stateful. That means in each QML file you import a JavaScript file, it will be imported with a new instance and the calculation or operation done or the value stored by the JavaScript will be true/saved for that particular QML file.

    If any other QML file imports the same JavaScript file, then another instance of the same JavaScript file will be created for this QML file, with all the variables' value set to default value, that is why you are getting 0.

    For further reading you can check here.

    Also if you use ".pragma library" to make it stateless, then these files cannot access QML component instance objects or properties directly.

    So, to solve your current problem, the JavaScript file has to be imported only once. Access that JavaScript file instance from any other QML file, under the same QML project.

    Regards,
    Sauvik

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    210

    Re: Troubles setting js global variables from QML file

    Thank you so much!
    It seems so obvious... now...
    The js file needed to be imported only into main, and then I could access it through main from all the other qml files.
    Cheers!
    Jon

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    31

    Re: Troubles setting js global variables from QML file

    Hi,

    Sorry to resurrect this thread, but how exactly did you do the "import in main and call it from other QML files"?

    I would like to use my Javascript game logic from several QML files as well, but can't seem to get it right.
    In my main QML file that contains a few other QML elements, I import my javascript file (import "name.js" as Hello). But if I try to access it from the other QML elements I just get "ReferenceError: Can't find variable: Hello)

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    210

    Re: Troubles setting js global variables from QML file

    You need to call a specific function in the js file. So say you have a function in your js file called "setup(data)" you can access that function from any other qml in the project simply by calling it, example:
    Code:
    Component.onCompleted: screen = setup(data)
    Or if in main, using your example above you could call it like this:
    Code:
    function doIt(data){Hello.setup(data)}
    Hope that helps!
    Cheers,
    Jon

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    31

    Re: Troubles setting js global variables from QML file

    Thanks for the quick response Jon, it's working perfectly now.

Similar Threads

  1. How to call/load another QML file from QML ?
    By cadlg in forum [Archived] Qt Quick
    Replies: 5
    Last Post: 2011-07-28, 09:15
  2. QML external JS file not found
    By anothertest in forum Qt
    Replies: 1
    Last Post: 2010-04-13, 11:33
  3. Replies: 5
    Last Post: 2007-01-31, 09:12
  4. Global variables???
    By leomil in forum Symbian
    Replies: 2
    Last Post: 2006-03-08, 08:24
  5. global variables
    By Nokia_Archive in forum Symbian
    Replies: 1
    Last Post: 2002-06-02, 04:20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×