I set my item delegates like so:
COMBOBOX_ITEMS_FRUITS = ['Apple', 'Banana']
COMBOBOX_ITEMS_COLORS = ['Red', 'Green', 'Blue']
self.treeview.setItemDelegateForColumn(COLUMN_A, ComboBoxDelegate(COMBOBOX_ITEMS_FRUITS))
self.treeview.setItemDelegateForColumn(COLUMN_B, ComboBoxDelegate(COMBOBOX_ITEMS_COLORS))
After the model is set as the source model for the proxy model, my app crashes but no errors were thrown:
self.model_source = treeview_model
self.sf_proxy_model.setSourceModel(self.model_source)
It seems that I can only use one setItemDelegateForColumn
when a sortfilterproxymodel is used to process the source model.
ComboBoxDelegate
is defined as follows:
class ComboBoxDelegate(QStyledItemDelegate):
def __init__(self, items):
super(ComboBoxDelegate, self).__init__()
self.items = items
def createEditor(self, parent, option, index):
editor = QComboBox(parent)
editor.setAutoFillBackground(True)
for item in self.items:
editor.addItem(item)
return editor
def setEditorData(self, editor, index):
current_index = editor.findText(index.model().data(index), Qt.MatchExactly)
editor.setCurrentIndex(current_index)
def setModelData(self, editor, model, index):
item_index = model.mapToSource(index)
item = model.sourceModel().item(item_index.row(), 0)
if index.parent().row() == -1 and item.hasChildren():
for row in range(item.rowCount()):
child = item.child(row, 3)
child.setText(editor.currentText())
model.setData(index, editor.currentText())
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
The treeview does not take ownership of the delegate, so you must keep a reference to it yourself (otherwise it will be garbage-collected by python):
self.delegate1 = ComboBoxDelegate(COMBOBOX_ITEMS_FRUITS)
self.delegate2 = ComboBoxDelegate(COMBOBOX_ITEMS_COLORS)
self.view.setItemDelegateForColumn(COLUMN_A, self.delegate1)
self.view.setItemDelegateForColumn(COLUMN_B, self.delegate2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With