5
votes

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 AA.qml page loaded through Loader drawLineLoaderA?

2

2 Answers

6
votes

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 
    }
}
2
votes

In addition to what @TheIndependentAquarius said, you can declare property of the corresponding type in your loader:

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 clearly stated that you deal with item of type AA and no other, and you'll get autocompletion on loaded item's properties as a bonus.


Note 1: Configuration of loaded item's properties should be done either in AA.qml itself (default values) or in Loader's onLoaded handler, as @troyane suggested.

Note 2: In your AA.qml you declared property string lineColour. You might be interested in color QML type. If you declare property color lineColour, QML will check that you assign valid values to this property. Moreover, color value is automatically converted to QColor when passed to C++ (and from QColor when passed from C++, of course).