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. Listview currentIndex increments after inserting item in model
Qt 6.11 is out! See what's new in the release blog

Listview currentIndex increments after inserting item in model

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.8k 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.
  • L Offline
    L Offline
    literA2
    wrote on last edited by literA2
    #1

    Hi,

    Did anyone noticed this issue?

    Btw, what i am trying to do is update the element, i used remove() and then insert() to update the currentItem, however after the insertion the currentIndex incremented. I wanted to remain the currentIndex to the previously selected item.

    Thanks.

    1 Reply Last reply
    0
    • CharbyC Offline
      CharbyC Offline
      Charby
      wrote on last edited by
      #2

      Hi,
      I don't think this is an issue but a desired behavior when you insert an item in the model before the current itemcurrentIndex to avoid changing the currentItem.
      Why don't you use set method to update the currentItem instead of removing/inserting ?

      Here is a small example showing insert/remove/set/append :

      import QtQuick 2.0
      import QtQuick.Window 2.0
      import QtQuick.Controls 1.4
      
      Window{
          visible:true;
          width:640; height:480;
      
          Component{
             id:controlPanel
             Flow{
      
                 width: parent.width
                 height:50
                 TextField{
                     id:name
      
                 }
                 Button{
                     text:"Add head"
                     onClicked: {
                         lstModel.insert(0,{"text":name.text})
                     }
                 }
                 Button{
                     text:"Add tail"
                     onClicked: {
                         lstModel.append({"text":name.text})
                     }
                 }
                 Button{
                     text:"Update current"
                     onClicked: {
                         lstModel.set(lstView.currentIndex, {"text":name.text})
                     }
                 }
                 Button{
                     text:"Current index:"+lstView.currentIndex+ (lstView.highlightFollowsCurrentItem ? "(follow)":"")
                     onClicked: {
                         lstView.highlightFollowsCurrentItem =!lstView.highlightFollowsCurrentItem
                     }
                 }
      
                 Button{
                     text:"previous"
                     onClicked: lstView.decrementCurrentIndex()
                 }
                 Button{
                     text:"next"
                     onClicked: lstView.incrementCurrentIndex()
                 }
                 Button{
                     text:"Remove selected"
                     onClicked: {
                         if (lstView.currentIndex!= -1){
      
                             lstModel.remove(lstView.currentIndex,1);
                             lstView.currentIndex = -1;
                         }
                     }
                 }
                 Button{
                     text:"unselected"
                     onClicked: lstView.currentIndex = -1;
                 }
      
             }
      
          }
      
      
          ListModel{
              id:lstModel
              ListElement{
                  text:"one item"
              }
              ListElement{
                  text:"another one"
              }
          }
      
          ListView{
              id:lstView
              anchors.fill: parent
              header: controlPanel
              model:lstModel
      
              delegate:Text{
                  text: model.text
                  MouseArea{
                      anchors.fill: parent
                      onClicked: lstView.currentIndex = index
                  }
              }
              highlight:Rectangle{
                  color:lstView.currentItem ? "blue" : "transparent"
              }
          }
      }
      
      
      L 1 Reply Last reply
      1
      • CharbyC Charby

        Hi,
        I don't think this is an issue but a desired behavior when you insert an item in the model before the current itemcurrentIndex to avoid changing the currentItem.
        Why don't you use set method to update the currentItem instead of removing/inserting ?

        Here is a small example showing insert/remove/set/append :

        import QtQuick 2.0
        import QtQuick.Window 2.0
        import QtQuick.Controls 1.4
        
        Window{
            visible:true;
            width:640; height:480;
        
            Component{
               id:controlPanel
               Flow{
        
                   width: parent.width
                   height:50
                   TextField{
                       id:name
        
                   }
                   Button{
                       text:"Add head"
                       onClicked: {
                           lstModel.insert(0,{"text":name.text})
                       }
                   }
                   Button{
                       text:"Add tail"
                       onClicked: {
                           lstModel.append({"text":name.text})
                       }
                   }
                   Button{
                       text:"Update current"
                       onClicked: {
                           lstModel.set(lstView.currentIndex, {"text":name.text})
                       }
                   }
                   Button{
                       text:"Current index:"+lstView.currentIndex+ (lstView.highlightFollowsCurrentItem ? "(follow)":"")
                       onClicked: {
                           lstView.highlightFollowsCurrentItem =!lstView.highlightFollowsCurrentItem
                       }
                   }
        
                   Button{
                       text:"previous"
                       onClicked: lstView.decrementCurrentIndex()
                   }
                   Button{
                       text:"next"
                       onClicked: lstView.incrementCurrentIndex()
                   }
                   Button{
                       text:"Remove selected"
                       onClicked: {
                           if (lstView.currentIndex!= -1){
        
                               lstModel.remove(lstView.currentIndex,1);
                               lstView.currentIndex = -1;
                           }
                       }
                   }
                   Button{
                       text:"unselected"
                       onClicked: lstView.currentIndex = -1;
                   }
        
               }
        
            }
        
        
            ListModel{
                id:lstModel
                ListElement{
                    text:"one item"
                }
                ListElement{
                    text:"another one"
                }
            }
        
            ListView{
                id:lstView
                anchors.fill: parent
                header: controlPanel
                model:lstModel
        
                delegate:Text{
                    text: model.text
                    MouseArea{
                        anchors.fill: parent
                        onClicked: lstView.currentIndex = index
                    }
                }
                highlight:Rectangle{
                    color:lstView.currentItem ? "blue" : "transparent"
                }
            }
        }
        
        
        L Offline
        L Offline
        literA2
        wrote on last edited by
        #3

        @Charby Thanks!

        Wow how can I miss the set() property.

        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