Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access elements in a ListView's delegate?



  • I have a ScrollView containing a ListView with a FolderListModel and some checkboxes like this. This gives a scrollable filesystem list with each row having a checkbox to the right.

    How can I access the checkboxes checked member for a given folderModel index (which is a certain row in the listView)? I'd like to check/uncheck certain checkboxes automatically.

    Thanks a lot!

    Window {
        visible: true
        width: 1920
        height: 1080
    
        FolderListModel {
            id: folderModel
            folder: "file:///home/pi/projects"
            rootFolder: "file:///home/pi/projetcs"
            showDirsFirst: true
            showDotAndDotDot: false
        }
    
        ScrollView {
            anchors.top: parent.top
            anchors.topMargin: 20
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            anchors.leftMargin: 20
            width: 800
            verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
            horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
    
            ListView {
                id: listView
                model: folderModel
    
                delegate: RowLayout {
                    Rectangle {
                        Layout.leftMargin: 5
                        Layout.rightMargin: 15
                        color: folderModel.isFolder(index) ? "#666666" : "#AAAAAA"
                        border.width: 1
                        border.color: "#000000"
                        radius: 4
                        width: 650
                        height: 65
    
                        Text {
                            leftPadding: 10
                            height: parent.height
                            width: parent.width
                            color: "#FFFFFF"
                            y: parent.height / 2 - font.pixelSize / 2
                            text: fileName
                            bottomPadding: 10
                        }
                    }
    
                    CheckBox {
                        style: CheckBoxStyle {
                            indicator: Rectangle {
                                implicitWidth: 35
                                implicitHeight: 35
                                radius: 3
                                border.width: 1
    
                                Rectangle {
                                    visible: control.checked
                                    color: "lime"
                                    border.color: "#000000"
                                    radius: 1
                                    anchors.margins: 4
                                    anchors.fill: parent
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    


  • hi,
    @t0m-n said in How to access elements in a ListView's delegate?:

    I'd like to check/uncheck certain checkboxes automatically.

    You can do like this :

      property variant checkedBoxes : [true,false,true,false,true,false,true,false]...
    
      CheckBox {
           checked:checkedBoxes[index]
      }
    

    instead of accessing the objects directly.
    Then you just modify checkedBoxes .

    
     var newValues = []
     for (var i in checkedBoxes){
                    newValues[i] = false
                }
     checkedBoxes = newValues
            
    


  • Thanks, that helped!


Log in to reply