Qt 一次取消选中/选中所有复选框

Qt 一次取消选中/选中所有复选框,qt,checkbox,qml,Qt,Checkbox,Qml,我想在选中/取消选中其他复选框时选中/取消选中所有剩余复选框(all option-列表视图中的第一个复选框) 在QML中,这些操作是使用ID处理的,因为我的Listview是从模型动态生成的,所以我无法为其他复选框生成ID。当前,“我的代码”仅选中/取消选中列表中的第一个复选框。请告诉我我做错了什么 import QtQuick 2.12 import QtQuick.Controls 2.4 import QtQuick.Layouts 1.3 Page { id : somep

我想在选中/取消选中其他复选框时选中/取消选中所有剩余复选框(
all option
-列表视图中的第一个复选框)

在QML中,这些操作是使用ID处理的,因为我的Listview是从
模型
动态生成的,所以我无法为其他复选框生成ID。当前,“我的代码”仅选中/取消选中列表中的第一个复选框。请告诉我我做错了什么

import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3

Page {

    id : somepageid

    ListView{

        id: multiSelectCheckList
        model: ["1", "2", "3", "4"]
        height: parent.height
        width: parent.width

        delegate: Column{

            height: 20
            width: parent.width


            // All checkbox option

            Loader{

                id: loaderAll
                height: 15
                active: model.row === 0
                sourceComponent:

                    CheckBox {
                    checked: true
                    text: "All"
                    indicator.width: 15
                    indicator.height: 15

                    onCheckStateChanged: {
                        // Here I want the check/uncheck feature
                        console.log("All", checked)

                        if(checked){
                            modelCheckBoxes.checked = true
                        } else{
                            modelCheckBoxes.checked = false
                        }
                    }
                }
            }

            // These checkboxes need to be check/uncheck
            // on clicking the above checkbox
            CheckBox {

                id: modelCheckBoxes
                anchors.top: loaderAll.bottom
                checked: true
                text: modelData
                indicator.width: 15
                indicator.height: 15

            }

        }
    }


}

您可能想看看这里:

复选框的第二个示例演示了如何在
按钮组的帮助下实现所需的效果:

Page {
    id : somepageid

    ButtonGroup {
        id: childGroup
        exclusive: false
        checkState: mainCheckBox.checkState
    }

    CheckBox {
        id: mainCheckBox
        checked: true
        text: "All"
        indicator.width: 15
        indicator.height: 15
        checkState: childGroup.checkState
    }

    ListView {
        id: multiSelectCheckList
        model: ["1", "2", "3", "4"]
        height: parent.height
        width: parent.width
        anchors {
            top: mainCheckBox.bottom
            margins: 10
        }

        delegate: CheckBox {
            id: modelCheckBoxes
            checked: true
            text: modelData
            indicator.width: 15
            indicator.height: 15
            ButtonGroup.group: childGroup
        }
    }
}