如何为代理模型处理的模型中的多个列设置项目委托?
How do I set item delegates for multiple columns in a model that is processed by a proxy model?
我这样设置我的项目代表:
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))
将模型设置为代理模型的源模型后,我的应用程序崩溃但未抛出任何错误:
self.model_source = treeview_model
self.sf_proxy_model.setSourceModel(self.model_source)
好像只能用一个setItemDelegateForColumn
当一个sortfilterproxymodel处理source model的时候
ComboBoxDelegate
定义如下:
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)
树视图不拥有委托的所有权,因此您必须自己保留对它的引用(否则它将被 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)
我这样设置我的项目代表:
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))
将模型设置为代理模型的源模型后,我的应用程序崩溃但未抛出任何错误:
self.model_source = treeview_model
self.sf_proxy_model.setSourceModel(self.model_source)
好像只能用一个setItemDelegateForColumn
当一个sortfilterproxymodel处理source model的时候
ComboBoxDelegate
定义如下:
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)
树视图不拥有委托的所有权,因此您必须自己保留对它的引用(否则它将被 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)