Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

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

    QML and Qt Quick
    listview currentindex
    2
    2
    7082
    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.
    • Niltava
      Niltava last edited by Niltava

      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 Reply Quote 0
      • ODБOï
        ODБOï last edited by ODБOï

        @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 Reply Quote 0
        • First post
          Last post