你有问题是因为你的
MyCheckBox
将两者分类
是
一
QCheckBox
(通过继承)以及
有
一
Q复选框
通过构建新的
Q复选框
实例在其init中(
self.cb
).
你真的只想做其中之一。为了演示,我重写了MyCheckBox类,如下所示:
class MyCheckBox(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.cb = QtGui.QCheckBox(parent)
cbLayout = QtGui.QHBoxLayout(self)
cbLayout.addWidget(self.cb, 0, QtCore.Qt.AlignCenter)
self.cb.clicked.connect(self.amClicked)
clicked = QtCore.Signal()
def amClicked(self):
self.clicked.emit()
这解决了问题(尽管您也需要进行一些其他更改)。
请注意,您使用的单击信号需要来自
我的复选框
而不是
Q复选框
所以我已经通过
amClicked
狭槽您不需要区分
data()
和
checked_state()
方法,所以我将它们合并为一个:
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid():
return None
elif role == QtCore.Qt.DisplayRole:
attr_name = self.columns[index.column()]
row = self.rows[index.row()]
return getattr(row, attr_name)
elif role == QtCore.Qt.CheckStateRole:
return None
else:
return None
然后代理看起来像这样。我只安排了一个编辑器,如果标志说它是可编辑的。如果没有,那么它负责绘制,因此它也必须在绘制方法中做正确的事情。
class CheckBoxDelegate(QtGui.QItemDelegate):
"""
A delegate that places a fully functioning QCheckBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
if not (QtCore.Qt.ItemIsEditable & index.flags()):
return None
cb = MyCheckBox(parent)
cb.clicked.connect(self.stateChanged)
return cb
def setEditorData(self, editor, index):
""" Update the value of the editor """
editor.blockSignals(True)
editor.setChecked(index.data())
editor.blockSignals(False)
def setModelData(self, editor, model, index):
""" Send data to the model """
model.setData(index, editor.isChecked(), QtCore.Qt.EditRole)
def paint(self, painter, option, index):
value = index.data()
if value:
value = QtCore.Qt.Checked
else:
value = QtCore.Qt.Unchecked
self.drawCheck(painter, option, option.rect, value)
self.drawFocus(painter, option, option.rect)
@QtCore.Slot()
def stateChanged(self):
print "sender", self.sender()
self.commitData.emit(self.sender())
另一种方法是使用继承而不是包含/委派。下面是一个使用的示例:
class MyCheckBox(QtGui.QCheckBox):
def __init__(self, parent):
QtGui.QCheckBox.__init__(self, parent)
class CheckBoxDelegate(QtGui.QItemDelegate):
"""
A delegate that places a fully functioning QCheckBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
这似乎更直接,然而,在这种情况下,它有几个问题。很难在
我的复选框
类-这需要我们重写
paintEvent
要做到这一点,需要仔细绘制。它也不会完全覆盖代理的绘制。所以你可以把它拿出来。但是,只有在为该行创建了编辑器时,它才能工作。因此,在这种情况下,第一种解决方案可能是最简单的。