4
votes

I have one QML file

QMLFile1.qml

Rectangle{   

    id: LogicFile
    property int  characters
    property bool checked

}

In second QML file QMLFile2.qml

If I try and instantiate the first file in a second file like this

Rectangle{

   QMLFile1{

         // unable to access the 
         //properties here id: LogicFile
         // property int  characters
         //property bool checked
   }

}

Why I am unable to access the properties of first QMLFile inside the second.

However if I instantiate the firstQML file in the second file directly i.e., not inside any element like rectangle , item etc all the properties are accessible why is it so?

1

1 Answers

5
votes

You can access the properties in the second file, for that:

  1. Expose the id of your parent element using Property alias to the outside world.
  2. Now redefine the id with the aliased id in the second file
  3. Now you can access them using aliased id name and and .(dot) operator

Example:

QMLFile1.qml

Rectangle{
    id: LogicFile
    property alias rectId:LogicFile // exposing id to outside files
    property int  characters
    property bool checked
}

QMLFile2.qml

Rectangle{

   QMLFile1{

           id:rectId       

          //Now you can access them like rectId.characters/rectId.checked
          rectId.characters = 10 

   }

}