Python(PySide,PyQt)的解决方案以使复选框居中,并允许进行编辑:
class BooleanDelegate(QItemDelegate):
def __init__(self, *args, **kwargs):
super(BooleanDelegate, self).__init__(*args, **kwargs)
def paint(self, painter, option, index):
value = index.data(Qt.CheckStateRole)
self.drawCheck(painter, option, option.rect, value)
self.drawFocus(painter, option, option.rect)
def editorEvent(self, event, model, option, index):
if event.type() == QEvent.MouseButtonRelease:
value = bool(model.data(index, Qt.CheckStateRole))
model.setData(index, not value)
event.accept()
return super(BooleanDelegate, self).editorEvent(event, model, option, index)
在您的表格模型中,确保标记允许用户勾选/取消勾选单元格。
class MyTableModel(QAbstractTableModel):
...
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
if index.column() in self.columns_boolean:
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
return Qt.ItemFlags(QAbstractTableModel.flags(self, index) | Qt.ItemIsEditable)
最后,在您的表格中设置BooleanDelagate
self.boolean_delegate = BooleanDelegate()
self.input_gui.setItemDelegateForColumn(5, self.boolean_delegate)