jdl
jdl

Reputation: 6323

Incrementing property variable in a loop

Not sure how to increment "something" in a Repeater loop in QML: variables, properties.
I would prefer to not to have to involve a wrapper to a cpp function.

property int bs: 0
Repeater {
    model:  12

    bs: {
        var vbs = bs + 1; //this works
        vbs;  
    }//ERROR:  Cannot assign to non-existent property bs
}

    Repeater{
        model: 7

        Row {
            spacing: table.rowSpacing

            DiagValue{
                value: {trans("sw " + (index + 1))}
                width: 60
            }

            Repeater{
                model:  12

                CheckBox {
                    id:myCheckbox
                    width: 50
                    height: 50
                    backingVisible: false
                    checkable: true

                    onClicked:{
                        matrix[i_index? ][j_index? ] = myCheckbox.checked   //how to do this assignement??

                        //pass the matrix to a cpp wrapper.
                    }
 OR
                    onClicked:{
                        matrix[i] = myCheckbox.checked   //how to do this assignement??
                        i++;//??

                        //pass the matrix to a cpp wrapper.
                    }


                }
            }
        }

Upvotes: 1

Views: 2288

Answers (1)

koopajah
koopajah

Reputation: 25602

You are trying to use the index property of both repeater at the same time. I would do something like this:

Column {
    Repeater{
        model: 7

        Row {
            id: currentRow
            // Store the index of the current row based on the
            // index in the first Repeater
            property int rowIndex: index
            spacing: 10
            Repeater{
                id: repeaterColumns
                model:  5

                CheckBox {
                    id:myCheckbox
                    width: 50
                    height: 50
                    // currentRow.rowIndex is the line index
                    // index is the column index
                    text: "Line " + currentRow.rowIndex + " col " + index
                }
            }
        }
    }
}

I don't really get where matrix comes from and if it is a 2D array or 1D array element. One of these should work:

onClicked:{
    matrix[currentRow.rowIndex][index] = myCheckbox.checked
}

or

onClicked:{
    matrix[currentRow.rowIndex * repeaterColumns.count + index] = myCheckbox.checked
}

The idea here is not to try and increment anything yourself but rely on the proper index properties of your element.

Upvotes: 1

Related Questions