pyQt: query checkbox checked in a QListWidget

user501743 picture user501743 · Jun 8, 2011 · Viewed 7.5k times · Source

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

Answer

Stephen Terry picture Stephen Terry · Jun 8, 2011

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()