[Interest] How to expose a ‘index’ property to a delegate?

Federico Ferri federico.ferri.it at gmail.com
Mon Nov 23 17:49:17 CET 2020


Following your advice, I changed Array.qml to the following: (most notably:
added the repeater.model property alias)

Entity {

    id: root

    property alias model: repeater.model


    property int length: 1

    property Transform transform: Transform {}

    property Component itemDelegate


    readonly property int firstIndex: 0

    property Component firstItemDelegate


    readonly property int lastIndex: length - 1

    property Component lastItemDelegate


    NodeInstantiator {

        id: repeater

        model: root.length


        delegate: Entity {

            id: repeatedEntity

            property int index: model.index


            components: [

                Transform {

                    matrix: {

                        var m = Qt.matrix4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)

                        for(var i = 0; i < repeatedEntity.index; i++)

                            m = m.times(root.transform.matrix)

                        return m

                    }

                }

            ]


            NodeInstantiator {

                delegate: repeatedEntity.index == root.firstIndex &&
firstItemDelegate

                            ? firstItemDelegate

                            : repeatedEntity.index == root.lastIndex
&& lastItemDelegate

                              ? lastItemDelegate

                              : itemDelegate

            }

        }

    }

}


However, model.index doesn’t work in the delegate, e.g.:

Array {

    id: root

    length: 10

    transform.scale: 0.95

    transform.translation.y: 0.5

    itemDelegate: Entity {

        components: [

            CuboidMesh {

                xExtent: 1.0; yExtent: 1.0; zExtent: 1.0;

            },

            PhongMaterial {

                diffuse: Qt.rgba(model.index / 10, 0.5, 0.5, 1.0)

                shininess: 75

            }

        ]

    }

}


(all items have the same Qt.rgba(1.0, 0.5, 0.5, 1) color)
Also changing the model reference (in PhongMaterial) to root.model.index
gives the same result.

Cheers,
Federico Ferri

On 23 November 2020 at 16:53:37, Giuseppe D'Angelo via Interest (
interest at qt-project.org) wrote:

Il 23/11/20 14:20, Federico Ferri ha scritto:
>
> but of course in this example it is using a non-existent ‘index’ in the
> PhongMaterial.diffuse binding.

Are you sure it's non-existing? The context in which that delegate is
going to be created should have "index" injected into it.

Needless to say, all of this is deprecated in 5.15 (in favour of
required properties), and even before you were never supposed to use
unqualified properties like those. You were supposed to

1) always prefix them with model.
2) expose properties on the root of the delegate, use them internally as
needed, and bind them when using the delegate in a view.

See > https://youtu.be/vzs5VPTf4QQ?t=2240

HTH,

-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

_______________________________________________
Interest mailing list
Interest at qt-project.org
https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20201123/43681a71/attachment.html>


More information about the Interest mailing list