declare global property in QML for other QML files

前端 未结 6 516
孤街浪徒
孤街浪徒 2020-11-27 17:53

I want to declare a global property in a config file and use it in other files. for example declare mainbg in:

Style.qml:



        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 18:18

    You can create a js file and import it to all of the files that have to use this property.

    js file:

    //Note: you only need '.pragma library' if you are planning to
    //change this variable from multiple qml files
    .pragma library
    var globalVariable = 20;
    

    qml file:

    import "test.js" as Global
    
    Rectangle {
      id: main
      width: 300; height: 400
    
      Component.onCompleted: {
        console.log( Global.globalVariable)
        //you can also change it
        Global.globalVariable = 5
      }
    }
    

提交回复
热议问题