Search code examples
pyqtqlistwidget

pyQt: query checkbox checked in a QListWidget


I am adding Checkboxes to a QlistWidget like this

item = QtGui.QListWidgetItem(listWidget)
ch = QtGui.QCheckBox()
listWidget.setItemWidget(item, ch)

like here Can't change state of checkable QListViewItem with custom widget

but i am somehow unable to get the checkbox item back at the time i want to know if they are checked by the user. I must be missing something very basic...

for index in xrange(listWidget.count()): 
    it=listWidget.itemAt(index,0)

So i need to know which checkboxes in the list the user checked? I cant figure what to do with the returned list item object to get the checkbox state. Should i use checkbox callbacks instead? Seems easier


Solution

  • I don't think you want to use itemAt to get the item. From the QListWidget docs:

    QListWidgetItem QListWidget.itemAt (self, int ax, int ay)

    Returns a pointer to the item at the coordinates (x, y).

    You probably want QListWidget.item(). Using that you can loop over the list items and get the check state like this

    for index in xrange(listWidget.count()):
        check_box = listWidget.itemWidget(listWidget.item(index))
        state = check_box.checkState()