I wish to be able to set the itemData
of a combobox
when populated using a QAbstractTableModel
. However, I can only return one string from the model's data
method.
Usually, when not using a model, this can be performed like so:
# Set text and data
combobox.addItem('Some text', 'some item data')
# Retrieve data from selected
item_data = combobox.itemData(combobox.currentIndex())
How can this be done, but using QAbstractTableModel
?
I have a combobox
, which I set a model to:
model = ProjectTableModel(projects)
combobox.setModel(model)
My model:
class ProjectTableModel(QtCore.QAbstractTableModel):
def __init__(self, projects=[], parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._projects = projects
def rowCount(self, parent):
return len(self._projects)
def columnCount(self, parent):
return 2
def data(self, index, role):
row = index.row()
column = index.column()
if role == QtCore.Qt.DisplayRole and column == 0:
project = self._projects[row]
name = project.name()
id = project.id() # <----- how to add this as itemData?
return name