Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any possibility to reach the index of an outer QML Repeater from the inner one (they are nested)?

I am trying to dynamically build a matrix of the same type of items in my QML application and keep it dynamic, so that you can change the number of rows and columns in a c++ file anytime. This has been working well, but now, to access them individually, I want to give them dynamic names. Therefore I nested two repeaters and tried to set the objectName as in the following:

    Repeater{
        id: rows
        model: Matrix1.row //number of rows in Matrix1-Object

        Repeater{
            id: columns
            model: Matrix1.column //number of columns in Matrix1-Object

            RepeatedItem{
                objectName: (index) +"."+ (rows.index) //matrix elements are 
                supposed to be numbered x.y because of the nested repeaters, e.g. 
                0.0 for the first element
            }
        }
    }

Unfortunately I seem to have no access to the outer index. The first value is shown, the second value is represented by the String undefined in the TextArea of my GUI. In case I add a new property to the outer Repeater and set it the same value as index, it will be set once and keep the first value (0) for each repeated row.

Is it impossible to dynamically reach this outer index value somehow? Or does anyone know a better way to dynamically create two-dimensional arrays of items in QML which are individually accessable?

like image 732
L. Srd Avatar asked Aug 08 '16 11:08

L. Srd


1 Answers

The index property is a context property. You can store it to an ordinary property, so you can access it from another context:

Repeater {
    id: rows
    // ...
    Repeater {
        id: columns
        property int outerIndex: index
        // ...
        Text {
            text: index + "." + columns.outerIndex
        }
    }
}
like image 58
jpnurmi Avatar answered Oct 29 '22 20:10

jpnurmi