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

Federico Ferri federico.ferri.it at gmail.com
Mon Nov 23 14:20:04 CET 2020


Consider a “basic” usage of NodeInstantiator:

NodeInstantiator {

    delegate: Entity {

        components: [SphereMesh {radius: index / 10}]

    }

}


We can use the ‘index’ property in delegate’s properties bindings.

Now consider this custom Qt3D Entity (Array.qml, repeats the delegate n
times, iteratively applying the specified transform, optionally using an
alternate delegate for first and/or last item):

import QtQml 2.12

import Qt3D.Core 2.12

import Qt3D.Render 2.12

import Qt3D.Extras 2.12


Entity {

    id: root

    property int n: 1

    property Transform t: Transform {}

    default property Component delegate

    property Component start

    property Component end


    NodeInstantiator {

        id: rep

        model: root.n


        delegate: Entity {

            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 < index; i++)

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

                        return m

                    }

                }

            ]

            NodeInstantiator {

                delegate: index == 0 && start

                            ? start

                            : index == (rep.model - 1) && end

                              ? root.end

                              : root.delegate

            }

        }

    }

}


How can this custom Entity provide an ‘index’ property to its
delegate? E.g. when used like this:


Array {

    n: 10

    t.scale: 0.95

    t.translation.y: 0.5

    delegate: Entity {

        components: [

            CuboidMesh {

                property real size: 1.0

                xExtent: size; yExtent: size; zExtent: size

            },

            PhongMaterial {

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

                shininess: 75

            }

        ]

    }

}


but of course in this example it is using a non-existent ‘index’ in
the PhongMaterial.diffuse binding.


-- 

Federico Ferri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20201123/2b7aab64/attachment.html>


More information about the Interest mailing list