How to get the index of selected row in ListView?



  • Hi All,

    I had displayed the listview, Now I want to know which list item [Row] has been selected by the user. By using the index of the row, so I can fetch from the value accordingly.

    Code Snippets:

     ListView {
            id: idListView
            anchors {
                left: parent.left
                leftMargin: 10 * scaleFactor
                right: parent.right
                rightMargin: 10 * scaleFactor
                top: rectangleToolBar.bottom
                topMargin: 10 * scaleFactor
                bottom: rectangleStatusBar.top
                bottomMargin: 10 * scaleFactor
            }
    
            model: objHomeController.detailsModel
    
            delegate: comsearchDelegate
          
            spacing: 10 * scaleFactor
            clip: true
    
            highlight:  Rectangle {
                color: 'grey'
                Text {
                    anchors.centerIn: parent
                    color: 'white'
                }
            }
            focus: true
        }
    
    
        Component {
    
            id: comsearchDelegate
          
    
            Row {
                spacing: 10 * scaleFactor
    
                 Column {
    
                Switch {
                    id: idswitch
                    checked: false
                    onCheckedChanged: {
                        
                      ****//Here I want to get which row has been selected, like Row number, CurrentIndex****
    
    
                        if(checked === true)
                        {
                            //Note: Get the Map Title/Description
                            console.log("**********CurrentIndex:");
                            console.log("**********CurrentIndex:",idListView.currentIndex);
                            console.log("**********currentItem:",idListView.currentItem);
    
                            intCurrentIndex = idListView.currentIndex;
    
                           // var varMapTitleCurrentIndex = varJsonString.Title[intCurrentIndex];
    
    
                        }
                        else if (checked === false)
                        {
                            //Note: Remove the ItemId from Array.
    
                        }
    
                    }
    
                }
                 }
                 Column {
    
                Image {
                    id: imageItem
                    width: 100 * scaleFactor
                    height: 70 * scaleFactor
                    source: thumbnailUrl
                }
                 }
    
                Column {
                    Layout.alignment: Qt.AlignTop
    
                    Text { text: title; font { pixelSize: 14  * scaleFactor; bold: true } }
                    Text { text: description; font { pixelSize: 14  * scaleFactor; bold: true } }
                    Text { text: modified; font { pixelSize: 14  * scaleFactor; bold: true } }
                    Text { text: size; font { pixelSize: 14  * scaleFactor; bold: true } }
    
                    MouseArea {
                                  id: idmusLstvw
                                  anchors.fill: parent
    
                                  onClicked:
                                  {
                                       console.debug("list click");
                                       idLoaderSize.setSource("capacity.qml")
    
                                 
                                   }
                              }
                        }
            }
        }
    

    Thanks In advance.


  • Moderators

    @Mathan-M

    ListView {
          onCurrentIndexChanged: {
                console.log(model.get(currentIndex))
          }
    }
    

  • Moderators

    @Mathan-M You can access the attached property named index inside the delegate of ListView.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.