I'm using a QAbstractTableModel
to populate a QComboBox
. This works great, but I wish to always have the very first combobox index to contain a value of "Select one...".
Is this possible, and if so - how?
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:
# Set the item's text
project = self._projects[row]
name = project.name()
return name
elif role == QtCore.Qt.UserRole and column == 0:
# Set the "itemData"
project = self._projects[row]
id = project.id()
return id