Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the public properties of a qml page loaded through a 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 AA.qml page loaded through Loader drawLineLoaderA?

like image 584
Aquarius_Girl Avatar asked Nov 27 '14 10:11

Aquarius_Girl


2 Answers

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 
    }
}
like image 199
Aquarius_Girl Avatar answered Oct 01 '22 03:10

Aquarius_Girl


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).

like image 41
tonytony Avatar answered Oct 01 '22 03:10

tonytony