Uncheck/Check all checkboxes at once
-
I want to check/uncheck all the remaining checkboxes on checking/unchecking of another checkbox (All option - the first checkbox in the listview).
In QML such actions are handled using IDs and since my Listview is dynamically generated from a model, I am unable to generate IDs for the other checkboxes. Currently, my code only checks/unchecks the first checkbox in the list. Please point me what am I doing wrong
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 } } } }
-
I want to check/uncheck all the remaining checkboxes on checking/unchecking of another checkbox (All option - the first checkbox in the listview).
In QML such actions are handled using IDs and since my Listview is dynamically generated from a model, I am unable to generate IDs for the other checkboxes. Currently, my code only checks/unchecks the first checkbox in the list. Please point me what am I doing wrong
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 } } } }
@chilarai hi
can this simplify the whole thing ?ListView{ id: multiSelectCheckList model: ["all","1", "2", "3", "4"] anchors.fill: parent delegate: Column{ CheckBox { text: modelData enabled: index === 0 checked: index === 0 ? true : multiSelectCheckList.itemAtIndex(0).children[0].checked } } }
also you are using anchors in a Column there must be warnings/errors saying
QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.
in your application output -
I want to check/uncheck all the remaining checkboxes on checking/unchecking of another checkbox (All option - the first checkbox in the listview).
In QML such actions are handled using IDs and since my Listview is dynamically generated from a model, I am unable to generate IDs for the other checkboxes. Currently, my code only checks/unchecks the first checkbox in the list. Please point me what am I doing wrong
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 } } } }
@chilarai said in Uncheck/Check all checkboxes at once:
I want to check/uncheck all the remaining checkboxes on checking/unchecking of another checkbox (All option - the first checkbox in the listview).
I think the best way to achieve this is to use
ExclusiveGroup
==> https://doc.qt.io/qt-5/qml-qtquick-controls-exclusivegroup.html -
@chilarai hi
can this simplify the whole thing ?ListView{ id: multiSelectCheckList model: ["all","1", "2", "3", "4"] anchors.fill: parent delegate: Column{ CheckBox { text: modelData enabled: index === 0 checked: index === 0 ? true : multiSelectCheckList.itemAtIndex(0).children[0].checked } } }
also you are using anchors in a Column there must be warnings/errors saying
QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.
in your application output -
@chilarai said in Uncheck/Check all checkboxes at once:
I want to check/uncheck all the remaining checkboxes on checking/unchecking of another checkbox (All option - the first checkbox in the listview).
I think the best way to achieve this is to use
ExclusiveGroup
==> https://doc.qt.io/qt-5/qml-qtquick-controls-exclusivegroup.html@KroMignon Thanks. I will look into this as well