4

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?

c00kiemonster
  • 22,241
  • 34
  • 95
  • 133
  • Do you mean you want to associate an object with each line of text? You'd probably be better using the model-view framework if you want to do this, or just associate the text with a python dict, perhaps stored in the QListWidget object itself. – xioxox Nov 03 '11 at 14:32

1 Answers1

9

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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336