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. Javascript shared variables between QML
Forum Updated to NodeBB v4.3 + New Features

Javascript shared variables between QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 1.7k Views 1 Watching
  • 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.
  • DjeffD Offline
    DjeffD Offline
    Djeff
    wrote on last edited by
    #1

    Hello,

    I have a first js file that get a list and fill a libraryModel after a Json request.

    When the user click on a row in the TableView the js file will send it to another js file that it set as a library (as I saw on internet, this ".pragma library" allow me to shared variable)

    But when I try to access to a variable in the library js file, I didnt receive what i wanted.

    My library js file :

    .pragma library
    
    var index;
    
    function setIndex(r){
        index = r;
        console.log("set row index : "+index)
    }
    
    function getIndex(){
        console.log("return index js : "+index)
        return index
    }
    
    

    I got this on the console :
    Click on my first QML file = qml: set row index : 1
    Access to my next QML file = qml: return index js : undefined

    Thank for your help

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

      Hi,

      How are you using js inside your second QML file? I think in your case you need something like this:

      import "path_to_your_js_file" as SharedLib
      //......
      Component.onCompleted: {
           console.log(SharedLib.index);
      }
      //.....
      

      By the way you don't need setter and getter methods because your variable is public.

      Mac OS and iOS Developer

      DjeffD 1 Reply Last reply
      0
      • shavS shav

        Hi,

        How are you using js inside your second QML file? I think in your case you need something like this:

        import "path_to_your_js_file" as SharedLib
        //......
        Component.onCompleted: {
             console.log(SharedLib.index);
        }
        //.....
        

        By the way you don't need setter and getter methods because your variable is public.

        DjeffD Offline
        DjeffD Offline
        Djeff
        wrote on last edited by
        #3

        @shav That's exactly what I have but it doesnt work

        1 Reply Last reply
        0
        • shavS Offline
          shavS Offline
          shav
          wrote on last edited by shav
          #4

          Why you use JS for sharing variable between qml files? You can use id of cml items inside your qml files. For example if you have qml file like this:

          import QtQuick 2.0
          
          Item {
              id: item1
              property var prop1: ({key1: "value1"})
          
              SomeAnotherQml {
              }
          }
          

          SomeAnotherQml.qml implementation:

          import QtQuick 2.0
          
          Item {
              id: secondQmlItem
          
              property var prop2: null
          
              Component.onCompleted: {
                  console.log("item1: "+JSON.stringyfire(item1.prop1));
                  console.log("prop2: "+JSON.stringyfire(prop2));
              }
          }
          

          In console you must see the value of prop1 from item1 also you can add property to your second qml file and send it like this:

          SomeAnotherQml {
              prop2: item1.prop1
          }
          

          Mac OS and iOS Developer

          DjeffD 1 Reply Last reply
          0
          • shavS shav

            Why you use JS for sharing variable between qml files? You can use id of cml items inside your qml files. For example if you have qml file like this:

            import QtQuick 2.0
            
            Item {
                id: item1
                property var prop1: ({key1: "value1"})
            
                SomeAnotherQml {
                }
            }
            

            SomeAnotherQml.qml implementation:

            import QtQuick 2.0
            
            Item {
                id: secondQmlItem
            
                property var prop2: null
            
                Component.onCompleted: {
                    console.log("item1: "+JSON.stringyfire(item1.prop1));
                    console.log("prop2: "+JSON.stringyfire(prop2));
                }
            }
            

            In console you must see the value of prop1 from item1 also you can add property to your second qml file and send it like this:

            SomeAnotherQml {
                prop2: item1.prop1
            }
            
            DjeffD Offline
            DjeffD Offline
            Djeff
            wrote on last edited by Djeff
            #5

            About your solution, I tried it before, many times, but I dont really know how to use it in my case and because I have to finish the project quickly, I dont have time to understand how use it.

            Thank for your help

            My problem is to set variable from js file to library js file, I dont know why it doesnt work

            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