Search code examples
pythonpyqt4qt-designer

Iterating all items inside QListView using python


I have a QlistView inside is which a checkboxes (created dynamically) with item name (QstandardItem). And below Qlistview is a checkbox named DatacheckercheckBox1. What I want is when this DatacheckercheckBox1 checkbox statechanges to "Checked", all the checkboxes inside the QlistView should be checked. I have made a signal for DatacheckercheckBox1 checkbox by

self.dlg.DatacheckercheckBox1.stateChanged.connect(self.selectAll)

i dont have idea in writing a method that should iterate all the items inside Qlistview and make the checkboxes next to it "Checked" if its not checked already.


Solution

  • Use the model to iterate over the items:

    model = self.listView.model()
    for index in range(model.rowCount()):
        item = model.item(index)
        if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked:
            item.setCheckState(QtCore.Qt.Checked)