Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] QML ListView behaviour when ListModel updates

    QML and Qt Quick
    2
    3
    3404
    Loading More Posts
    • 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.
    • B
      beemaster last edited by

      Hi everyone,

      I have a ListView
      @ListView {
      id: listView
      anchors.fill: parent
      model: model
      delegate: delegate
      orientation: ListView.Horizontal
      snapMode: ListView.SnapOneItem
      highlightRangeMode: ListView.StrictlyEnforceRange
      }@

      Each item in ListView is fullscreen:
      @Component {
      id: delegate
      Rectangle {
      width: component.width
      height: component.height
      Text {
      anchors.centerIn: parent
      font.pointSize: 40
      text: number
      }
      }
      }@

      At start model has just one item:
      @ListModel {
      id: model
      ListElement {
      number: 10
      }
      }@

      I want to modify model dynamically later on, like this:
      @Keys.onLeftPressed: {
      model.insert(0, {"number":leftIndex})
      --leftIndex
      }

      Keys.onRightPressed: {
      model.append({"number":rightIndex})
      ++rightIndex
      }@

      When currentIndex is 0 and I insert to beginning, my listView automatically jumps to newly inserted item. When currentIndex is different from 0, my listView stays on the same item during insertions to both ends.
      I want to keep the behaviour consistent. I want my listView to stay at currentItem during insertions to both sides. Is it possible?

      Thanks!

      1 Reply Last reply Reply Quote 0
      • T
        tolszak last edited by

        It's list view and list model so index 0 means beggining of the list.
        So it's proper behaviour:

        If you desire different behaviour you could:
        @
        Keys.onLeftPressed {
        model.insert(0,{"number":leftIndex}
        --leftIndex
        if (listView.currentIndex == 0 && model.count > 1) {
        listView.incrementCurrentIndex
        }
        }
        @

        1 Reply Last reply Reply Quote 0
        • B
          beemaster last edited by

          tolszak, that doesn't help, I've tried.

          Surprisingly I found an answer: I can not use
          @

          snapMode: ListView.SnapOneItem

          @

          together with

          @

          highlightRangeMode: ListView.StrictlyEnforceRange

          @

          So I ended up with this ListVew:
          @ListView {
          id: listView
          anchors.fill: parent
          model: model
          delegate: delegate
          orientation: ListView.Horizontal
          highlightRangeMode: ListView.StrictlyEnforceRange
          }@

          1 Reply Last reply Reply Quote 0
          • First post
            Last post