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. How to set currentIndex of ListView to value other than 0 at start?

How to set currentIndex of ListView to value other than 0 at start?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
listviewcurrentindex
2 Posts 2 Posters 8.5k 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.
  • N Offline
    N Offline
    Niltava
    wrote on 7 Dec 2017, 12:43 last edited by Niltava 12 Jul 2017, 12:45
    #1

    I have a ListView that lives inside a Component that is loaded / unloaded per demand. However the value selected by the user on the ListView is to be remembered so that the same value is selected when User visits next time. Below is a complete snippet that mimics my scenario.

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0

    ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    QtObject {
        id: internal
        property int contactIndex: 0
        property bool listLoaded: false
    }
    
    Button {
        id: button
        anchors.horizontalCenter: parent.horizontalCenter
        text: (!internal.listLoaded) ? "Show List" : "Hide List"
        onClicked: {
            loader.sourceComponent = (!internal.listLoaded) ? listViewComponent : undefined
            internal.listLoaded = !internal.listLoaded
        }
    }
    
    Loader {
        id: loader
        anchors.centerIn: parent
        onStatusChanged: if (loader.status == Loader.Ready) {
                             console.log("read current-index:", internal.contactIndex)
                             loader.item.model = contact1
                             loader.item.currentIndex = internal.contactIndex
                         }
    }
    
    Connections {
        target: loader.item
        onItemClicked: {
            internal.contactIndex = currentIndex
            console.log("saved current-index:", internal.contactIndex)
        }
    }
    
    Contacts {
        id: contact1
    }
    
    Component {
        id: listViewComponent
        ListView {
            id: listView
            width: 180; height: 200
            currentIndex: -1
    
            signal itemClicked(var currentIndex)
    
            model: undefined
    
            delegate: Text {
                id: text
                text: name + ": " + number
                color: ListView.isCurrentItem ? "red" : "black"
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        text.ListView.view.currentIndex = index
                        itemClicked(index)
                    }
                }
            }
    
            onCurrentIndexChanged: {
                console.log(listView, "current index changed to", currentIndex)
                // Some other operations need to go in here...
            }
        }
    }
    

    }

    Contacts.qml is following:

    import QtQuick 2.0

    ListModel {
    ListElement {
    name: "Bill Smith"
    number: "555 3264"
    }
    ListElement {
    name: "John Brown"
    number: "555 8426"
    }
    ListElement {
    name: "Sam Wise"
    number: "555 0473"
    }
    }

    The problem is that as soon as the model is set, the currentIndex of ListView is automatically set to 0. Is there a way to prevent currentIndex of ListView from being automatically set to 0 when model is assigned?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ODБOï
      wrote on 7 Dec 2017, 14:57 last edited by ODБOï 12 Jul 2017, 14:59
      #2

      @Niltava Hello,

      property int _index : 4

      ListView {
      id: listView
      width: 180; height: 200
      // currentIndex: _index
      Component.onCompleted : currentIndex = _index
      }
      like this your list will be set at index 4

      1 Reply Last reply
      0

      1/2

      7 Dec 2017, 12:43

      • Login

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