I implemented the following delegate, to provide a combobox in a QTableView. The use case is to replace a column (key) that is generally meaningless for the user (e.g. a numerical id) with a text equivalent.
The snippet below works (also for saving the proper value), but it has three issues:
- It displays the original value, instead of the text equivalent.
- A selection of rows in the QTableView provides all columns but not the one with this delegate.
- Ideally, I would like the combobox to appear as such, without the user having to click on it to find out it is one.
Note: The key can be any string (not necessarily an integer number). A typical example would be the country (the value "France" corresponds to the key "FR").
class ComboDelegate(QtGui.QItemDelegate):
"""
A delegate that places a QComboBox in every
cell of the column to which it is being applied
"""
def __init__(self, parent, value_data):
"Value_data is a list of tuples: (item, label)"
QtGui.QItemDelegate.__init__(self, parent)
self.value_data = value_data
@property
def items(self):
"The list of items for display"
return [item[0] for item in self.value_data]
@property
def labels(self):
"The list of labels for display"
return [item[1] for item in self.value_data]
def item(self, label):
"Get the item from a label"
try:
index = self.labels.index(label)
except ValueError:
pass
print("Value no: &%s" % index)
return self.items[index]
def createEditor(self, parent, option, index):
"Create the editor (called each time)"
combo = QtGui.QComboBox(parent)
for duplet in self.value_data:
# the duplet is label, item
item, label = duplet
combo.addItem(label)
combo.currentIndexChanged.connect(self.currentIndexChanged)
return combo
def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setCurrentIndex(index.row())
editor.blockSignals(False)
def setModelData(self, editor, model, index):
"This is the data stored into the field"
print("Current text: %s" % editor.currentText())
model.setData(index, self.item(editor.currentText()))
def currentIndexChanged(self):
self.commitData.emit(self.sender())
The first problem could most easily be solved by the model, i.e. it can provide the text for a certain value when asked for the
DisplayRole
and still provide the numerical value viaEditRole
.For displaying the combobox there are two options
when using a delegate, overwrite the
paint()
method to draw the combobox, e.g. by delegating to the current widget style just likeQComboBox
itself doesinstead of a delegate, set an index widget. see
QAbstractItemView::setIndexWidget()