[SOLVED] QML ListView behaviour when ListModel updates



  • 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!



  • 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
    }
    }
    @



  • 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
    }@


Log in to reply
 

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