Help with ListModel and setProperty

  • When I try to change a property value of an item contained into a ListModel the following code has no effect:


    import QtQuick 2.0
    Item {
        anchors.fill: parent
        ListModel { id: modelCrayon }
        Component.onCompleted: {
            for (var i = 0; i < 10; i++)
                modelCrayon.append( { _tag: i, _source: "resources/crayon-green.png", _selected: false } )
        Column {
            x: -170
            spacing: 0
            Repeater {
                model: modelCrayon
                delegate: Crayon {
                    tag: _tag
                    source: _source
                    selected: _selected
                    onCrayonSelected: {
                        for (var i = 0; i < modelCrayon.count; i++) {
                            if (i == tag) continue;
                            modelCrayon.setProperty(i, "_selected", false);


    import QtQuick 2.0
    Image {
        property bool selected
        property int tag
        signal crayonSelected()
        id: crayon
        smooth: true
        fillMode: Image.PreserveAspectFit
        onSelectedChanged: console.debug(tag, selected)
        MouseArea {
            anchors.fill: parent
            onClicked: {
                selected = !selected
                if (selected) crayonSelected()
        states: State {
            name: "selected"; when: selected == true
            PropertyChanges { target: crayon; x: 30 }
        transitions: Transition {
            from: ""; to: "selected"
            PropertyAnimation { property: "x"; duration: 500; easing.type: Easing.InOutQuad }

    Nothing is shown on console, so the "selected" var is never changed. I'm sure there's something obvious I'm missing.

    By the way, is there a smarter way to use a ListModel as a OptionBox? I mean I want only ONE item at time must have the selected property == true. Or, in other words, keep tracks of the selected index.

  • I wasn't able to fix the issue but I solved my problem using a ListView instead of a Column and setting the selected property as ListView.isCurrentItem in the delegate.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.