Use index in a ListView delegate

  • Hi,

    i hope you can help me to find out whether i have found a bug or not. I tried to use the index variable in a delegate within a ListView.

    @ListView {
    id: selectedItems
    width: childrenRect.width
    implicitHeight: currentItem ? currentItem.height : 0
    clip: true
    orientation: ListView.Horizontal
    model: breadCrumbModel
    delegate: Component {
    id: delegateComp
    Row {
    visible: ListView.isCurrentItem ? !mover.visible : true
    id: wrapper
    property var wrapperitemdata: (itemdata ? itemdata : parent.itemdata)
    Loader {
    sourceComponent: container.separator
    anchors.verticalCenter: breadcrumb.verticalCenter
    height: breadcrumb.height
    visible: (index == 0 ? false : true)
    MouseArea {
    anchors.fill: parent
    onClicked: container.separatorClicked(index)
    Loader {
    id: breadcrumb
    property var itemdata: wrapper.wrapperitemdata
    sourceComponent: container.breadCrumbDelegate
    anchors.fill: parent
    onClicked: container.breadCrumbClicked(index)

    This line 130 is relevant:
    @visible: (index == 0 ? false : true)@

    The code works good but i will get error to console:

    BreadCrumbBar.qml:130: ReferenceError: index is not defined

    Did i made something wrong?

  • It really looks OK so I'm not sure why you get this error. Maybe using Loader changes something or parenthesis are unnecessary. Very odd indeed.

  • The use of index is anyway a little bit strange. Have you ever tried to export the index resp. also the "currentIndex" and the function "isCurrentIndex" to a delegate out of the file where the ListView is defined? It seems that's absolutely impossible!

  • Use of index is completely valid, e.g. if you want to use different color for odd and even rows.

  • The visible line is wrong. You want:

    visible: (index == 0) ? false : true

    Also, you don't need to declare your delegate in a component, the delegate just needs to be given a delegate-derived type (i.e. just the Row would work fine)

  • Ok, thanks. It's fixed and thread can be closed.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.