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] How to restore ListView's selected item after the collection (model) changes?

[SOLVED] How to restore ListView's selected item after the collection (model) changes?

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 2 Posters 3.4k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Aralox
    wrote on last edited by
    #1

    Hey there, the title says it all.

    I have buttons that add and remove items from the QList<QObject*> that my ListView is bound to. The currentItem of the ListView reverts back to the first item whenever I add or remove items - is there a way to preserve this, at least for additions?

    Thanks

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Can you show how do you remove item ? Can you post the code ?

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aralox
        wrote on last edited by
        #3

        The relevant QML looks like this:

        @ Button{
        width: 100; height: 30
        text: "remove"
        onClicked: {
        myModel.removeThing(myListView.currentIndex)
        }
        }
        ...

        ListView {
        id: myListView
        anchors.fill: parent
        model: myModel.list

        delegate: Rectangle {
         width: 300;
         height: 30;
        
         Text {
          text: model.modelData.stuff
         }
        
                                            ..... [logic for selecting items].....
        
        }
        

        }

        @

        The C++ (within myModel):

        @ void removeThing(int index)
        {
        _list.removeAt(index);
        }@

        where _list is a QList<QObject*> which is exposed as a Q_PROPERTY that 'myListView' binds to.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          How did you set the currentIndex of the ListView ?

          157

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aralox
            wrote on last edited by
            #5

            in my delegate, I have this:

            @ MouseArea {
            anchors.fill: parent
            onClicked: {
            myListView.currentIndex = index
            }
            }@

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              So when you add you can "incrementCurrentIndex()":http://qt-project.org/doc/qt-5/qml-qtquick-listview.html#incrementCurrentIndex-method or "decrementCurrentIndex()":http://qt-project.org/doc/qt-5/qml-qtquick-listview.html#decrementCurrentIndex-method on removal.

              157

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Aralox
                wrote on last edited by
                #7

                ahh right thats so obvious, ive just saved and restored the index before and after my add now. Thanks!

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  You're welcome :)
                  You can mark the post as solved.

                  157

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Aralox
                    wrote on last edited by
                    #9

                    How do I do that? just put "[SOLVED]" in the title?

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      bq. just put “[SOLVED]” in the title?

                      Yes. This is how it works currently :)

                      157

                      1 Reply Last reply
                      0

                      • Login

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