How to access the public properties of a qml page loaded through Loader?

AA.qml

Item
{
    id:             drawLinesOnC

    property string  lineColour
    property int     lineDrawingSourceType
    property variant startEndPointArray

}

main.qml

Loader
{
   id:     drawLineLoaderA
   source: "AA.qml"
}

-

How to access the public properties of a page AA.qmlloaded through Loader drawLineLoaderA?

+4
source share
2 answers

The solution is as follows:

drawLineLoaderA.source = "DrawLineLoader.qml"
if (drawLineLoaderA.status == Loader.Ready)
{
    if (drawLineLoaderA.item && drawLineLoaderA.item.lineColour)
    {
        drawLineLoaderA.item.lineColour            = "black"
        drawLineLoaderA.item.lineDrawingSourceType = 2 
    }
}
+4
source

In addition to what @TheIndependentAquarius said, you can declare a property of the appropriate type in your bootloader:

Loader {
    id: drawLineLoaderA
    readonly property AA aa: item
    source: "AA.qml"
}

And then use it like this:

if (drawLineLoaderA.aa) {
    drawLineLoaderA.aa.color = "black"
}

Now you have clearly stated that you are dealing with an element of type AA and with nothing else, and you will receive autocomplete properties of the loaded objects as a bonus.


1. AA.qml( ), Loader onLoaded, @troyane.

2. AA.qml property string lineColour. color QML. property color lineColour, QML , . , color QColor ++ ( QColor ++, ).

+1

All Articles