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. GridVIew last item auto selected on insert

GridVIew last item auto selected on insert

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 265 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
    nminkov
    wrote on last edited by
    #1

    Hi,
    I am struggling with simple issue . Every time I insert an item in my GridView, the last inserted item is selected. That is, last inserted item automatically becomes currentItem/currentIndex.

    How can I avoid that?

    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by Shrinidhi Upadhyaya
      #2

      Hi @nminkov , i guess you must be handling some focus stuffs in the gridview, by default there is no auto select or auto highlight feature in GridView, it would be better if you would post your sample code.

      Here is a sample code please have a look into it:-

      ListModel {
      id: dummyModel

          ListElement {
              name: 0
          }
          ListElement {
              name: 1
          }
          ListElement {
              name: 2
          }
          ListElement {
              name: 3
          }
      }
      
      GridView {
          id: gridView
      
          width: parent.width;
          height: 200
      
          model: dummyModel
          delegate: Rectangle {
              width: 100
              height: 100
              color: "cyan"
              border.color: GridView.isCurrentItem ? "red" : "black"
              border.width: 4
      
              Text {
                  anchors.centerIn: parent
                  text: name
              }
      
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      gridView.currentIndex = index
                  }
              }
          }
      }
      
      Button {
         text: "Add Item"
         anchors.centerIn: parent
         onClicked: {
             dummyModel.append({"name": dummyModel.count})
         }
      }
      

      Sample Output:-
      200cf945-aa13-43fb-9e18-ba8345094571-image.png

      Note:- Highlighted Rectangle will have a red border, on clicking the Rectangle iam changing the current index of the gridview and on clicking on add item iam inserting a item into the model.

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      N 1 Reply Last reply
      5
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @nminkov , i guess you must be handling some focus stuffs in the gridview, by default there is no auto select or auto highlight feature in GridView, it would be better if you would post your sample code.

        Here is a sample code please have a look into it:-

        ListModel {
        id: dummyModel

            ListElement {
                name: 0
            }
            ListElement {
                name: 1
            }
            ListElement {
                name: 2
            }
            ListElement {
                name: 3
            }
        }
        
        GridView {
            id: gridView
        
            width: parent.width;
            height: 200
        
            model: dummyModel
            delegate: Rectangle {
                width: 100
                height: 100
                color: "cyan"
                border.color: GridView.isCurrentItem ? "red" : "black"
                border.width: 4
        
                Text {
                    anchors.centerIn: parent
                    text: name
                }
        
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        gridView.currentIndex = index
                    }
                }
            }
        }
        
        Button {
           text: "Add Item"
           anchors.centerIn: parent
           onClicked: {
               dummyModel.append({"name": dummyModel.count})
           }
        }
        

        Sample Output:-
        200cf945-aa13-43fb-9e18-ba8345094571-image.png

        Note:- Highlighted Rectangle will have a red border, on clicking the Rectangle iam changing the current index of the gridview and on clicking on add item iam inserting a item into the model.

        N Offline
        N Offline
        nminkov
        wrote on last edited by
        #3

        @Shrinidhi-Upadhyaya Thanks for the complete answer. It helped me track the issue to the insert function.

        In my model, I was using beginInsertRows(QModelIndex(), 0, 0), which inserted rows in front instead of back. It was unintended and caused to display the wrong text under every element. That made me believe the actual selected element was wrong.

        1 Reply Last reply
        2

        • Login

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