Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I attach objects to QListWidgetItems in PyQt?

Is it possible to set/get a object for an item in a QListWidget the same way you set/get the text of the item?

like image 517
c00kiemonster Avatar asked Jan 18 '23 09:01

c00kiemonster


1 Answers

There's QListWidgetItem.setData:

item = QListWidgetItem('Text', parent)
data = ('foo', 'bar', [1, 2, 3])
item.setData(Qt.UserRole, data)
...
# QVariant version 1 API (python2 default)
print item.data(Qt.UserRole).toPyObject()
# QVariant version 2 API (python3 default)
print item.data(Qt.UserRole)
like image 180
ekhumoro Avatar answered Jan 28 '23 16:01

ekhumoro