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. [SOLVED] QML ListView behaviour when ListModel updates

[SOLVED] QML ListView behaviour when ListModel updates

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.6k Views
  • 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 Offline
    B Offline
    beemaster
    wrote on 8 Oct 2012, 19:35 last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tolszak
      wrote on 11 Oct 2012, 13:05 last edited by
      #2

      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
      0
      • B Offline
        B Offline
        beemaster
        wrote on 12 Oct 2012, 06:52 last edited by
        #3

        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
        0

        1/3

        8 Oct 2012, 19:35

        • Login

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