Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Different delegates at the repeater



  • Hello everyone, could you help with little issue:
    I'd like to make a different delegates at the repeater,
    for example i have model like this:
    @ListModel {
    ListElement {
    name: "1"
    itemDelegate: "1.qml"
    }
    ListElement {
    name: "2"
    itemDelegate: "2.qml"
    }
    }@

    i try to use something like:
    @Repeater {
    id: repeater
    model: tabModel
    delegate: Qt.createComponent(Qt.resolvedUrl(itemDelegate))
    }@

    but have warning:
    "ReferenceError: itemDelegate is not defined"

    is it possible to make a different delegates at the Repeater component?


  • Moderators

    Hi,

    AFAIK the roles can be accessed only inside the component assigned to delegate. You can do following instead,
    @
    delegate: Item {
    id: item
    Component.onCompleted: {
    var component = Qt.createComponent(Qt.resolvedUrl(itemDelegate))
    var obj = component.createObject(item);
    obj.y = index*40
    }
    }
    @



  • tnx 4 answer!
    how i understand i should manualy calculate possition of new item?

    is it possible to use some anchors?
    currently i make this:
    @ delegate: Item {
    id: item
    width: children.width
    height: children.height
    Component.onCompleted: {
    var component = Qt.createComponent(Qt.resolvedUrl(itemDelegate))
    var obj = component.createObject(item);
    }
    }@
    but i saw only last item, as i understand i should manualy shift items in the repeater, but IMO this is some spike...

    is some way to correct solve this issue?



  • btw, don't know is it importent or not:
    i use
    @Flickable {
    anchors {
    top: header.bottom
    left: parent.left
    right: parent.right
    }
    height: 100
    contentWidth: row.width
    Row {
    id: row
    spacing: 1

            Repeater {
                id: repeater
                model: myModel
                delegate: Item {
                    id: factoryItem
                    width: children.width
                    height: children.height
                    Component.onCompleted: {
                        var component = Qt.createComponent(Qt.resolvedUrl(null != itemDelegate ? itemDelegate : "DefaultDelegate.qml"))
                        var obj = component.createObject(factoryItem);
                    }
                }
            }
        }
    

    }@


  • Moderators

    bq. how i understand i should manualy calculate possition of new item?

    createObject returns the object that is created. It can be used to assign values to its properties.
    @
    var obj = component.createObject(item);
    obj.y = index*40
    @

    Or (as you have already did) putting it in Row or Column should also work.


Log in to reply