0

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.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Gerome Schutte
  • 184
  • 1
  • 11
  • Works for me. Are you sure `readonly` is not specified? By the way, [`children`](http://doc.qt.io/qt-5/qml-qtquick-item.html#children-prop) is already a property of `Item`, i.e. you can simply declare your `ChildCustomControl`s inside `ParentCustomControl` and that's it, they are inside the `children` property inherited from `Item`. Clearly if you have other (custom) `Component`s that's not the way to go (and a possible approach would be [this](http://stackoverflow.com/a/31148671/2538363)). – BaCaRoZzo Jul 02 '15 at 13:21
  • I mistakenly used "children" as the name for the list property. I changed the names so that I don't give away what these components actually do, but inside my parent control this property is actually called `options` – Gerome Schutte Jul 02 '15 at 13:53
  • It is always good to not have name clashes. :) Anyhow, it worked for me even with `children`. That `readonly` issue is quite strange. Please provide a minimal version of your code to be tested. – BaCaRoZzo Jul 02 '15 at 13:59
  • 1
    @BaCaRoZzo I updated my post to show minimum testable code – Gerome Schutte Jul 02 '15 at 14:49
  • 1
    Is it `Container` a custom QML type? Like this it's really hard to help. – BaCaRoZzo Jul 02 '15 at 16:03
  • No. `XParent` and `XOption` are both written in pure QML, which means they are based off of pre-existing QML components, in this case both of them are based on the pre-existing Container component. – Gerome Schutte Jul 02 '15 at 19:23
  • 1
    Wait...it's cascades! You should specify in your question. Unfortunately I cannot test your code properly, sorry. Also, provide fully working code...parenthesis are unbalanced. – BaCaRoZzo Jul 02 '15 at 20:14

0 Answers0