Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Use index in a ListView delegate
Forum Updated to NodeBB v4.3 + New Features

Use index in a ListView delegate

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wolfer
    wrote on last edited by
    #1

    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
    MouseArea{
    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      daliusd
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wolfer
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daliusd
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stevenhurd
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wolfer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved