Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the index of selected row in ListView?
QtWS25 Last Chance

How to get the index of selected row in ListView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
listviewlistview numberrow
3 Posts 3 Posters 7.7k 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.
  • M Offline
    M Offline
    Mathan M
    wrote on last edited by p3c0
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • M Mathan M

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mathan-M

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

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

        157

        1 Reply Last reply
        1

        • Login

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