Getting around list<> properties being readonly

265 Views Asked by At

I have two custom components written in QML

//XOption.qml
Container {
    id: xOption
    property string title;
    property bool active: false;
    function makeActive() {active=true}
    onActiveChanged {
        //alter appearance of option to reflect whether active/not
    }

    onTouch {
        if (touchEvent reflects a tap) {
            //notify underlying c++ engine that I was tapped
            //engine will notify parent control that I was tapped by setting the flag var
        }
    }
    //label to display title and some other visual components
}

//XParent.qml
Container {
    id: XParent;
    property list<XOption> options;
    property int selectedOption: 0;
    property string flag: cppengine.flag;

    onCreationCompleted {
        for (var k = 0; k < children.length; ++k) {
            if (k==selectedOption)
                options[k].makeActive()
        }
        cppengine.declareParentage(options);
    }

    onFlagChanged {
        if (flag indicates that one of my child options was tapped) {
            //determine from flag which option was tapped
            tappedOption.makeActive()
            //make other options inactive
        }
    }
}

But now I want to use XParent in another QML document and assign any number of different XOptions to it like so:

Container {
    XParent {
        options: [
            XOption {
                title: "title1";
            },
            XOption {
                title: "title2";
            }
        ]
    }
}

However, when doing so, I get the error:

Invalid property assignment: "options" is a read-only property

Is there any way I could get around this? I've tried making options a string array type variant, that would contain the title of every child option to create, and then adding a ComponentDefinition for XOption to XParent and creating one for every title that was specified, but if I do that I am unable to call XOption's makeActive(), which is absolutely necessary.

0

There are 0 best solutions below