5

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.

Paco
  • 4,520
  • 3
  • 29
  • 53
harinish
  • 261
  • 3
  • 5
  • 17
  • I think below link can help you. Check it http://stackoverflow.com/questions/4629584/pyqt4-how-do-you-iterate-all-items-in-a-qlistwidget – Vickal May 06 '15 at 11:20
  • 1
    Yeah i do saw the link and i dont find any method relevant in the link for QListView. – harinish May 06 '15 at 11:30
  • I think @SAM refers to `.findItems` method. – ngulam May 06 '15 at 12:04
  • 1
    @ngulam. The `QListView` class doesn't have a `findItems` method. The `QStandardItemModel` class does, but it isn't really the right tool for the job, because it filters the items by matching text, rather than flags. – ekhumoro May 06 '15 at 16:01

1 Answers1

8

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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks for the response. Am new to PyQT. Sorry for silly question but i wana make me myself clear. Could you please tell me more about model. I don't find any object model in documentation and also what data type does model returns? Where does the rowCount method from? – harinish May 06 '15 at 17:14
  • 1
    @harinish. I assumed from your question that you are using a [QStandardItemModel](http://doc.qt.io/qt-4.8/qstandarditemmodel.html), which is a subclass of [QAbstractItemModel](http://doc.qt.io/qt-4.8/qabstractitemmodel.html). The [rowCount](http://doc.qt.io/qt-4.8/qstandarditemmodel.html#rowCount) function is in the [Reimplemented Public Functions](http://doc.qt.io/qt-4.8/qstandarditemmodel.html#reimplemented-public-functions) section. – ekhumoro May 06 '15 at 17:40
  • Can i know what are you doing in item = model.item(index)? – harinish May 07 '15 at 06:39
  • 1
    @harinish. It [gets the item](http://doc.qt.io/qt-4.8/qstandarditemmodel.html#item) at the given index. The returned item is a [QStandardItem](http://doc.qt.io/qt-4.8/qstandarditem.html), which contains the data for that row/index in the list-view. – ekhumoro May 07 '15 at 16:50
  • For those who are lazy add : ```elif item.isCheckable() and item.checkState() == QtCore.Qt.Checked: item.setCheckState(QtCore.Qt.Unchecked)```right after the ```if``` condition to have the double effect check all & uncheck all – PAYRE Quentin Nov 04 '20 at 22:06