我想将只读复选框委托到QTableWidget 中
我有下面一个复选框的类,它显示为已启用(可编辑)
from PySide import QtCore, QtGui
class CheckBoxDelegate(QtGui.QStyledItemDelegate):
"""
A delegate that places a fully functioning QCheckBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QStyledItemDelegate.__init__(self, parent)
self.parent = parent
def createEditor(self, parent, option, index):
'''
Important, otherwise an editor is created if the user clicks in this cell.
** Need to hook up a signal to the model
'''
return None
def paint(self, painter, option, index):
'''
Paint a checkbox without the label.
'''
checked = index.data() #.toBool()
check_box_style_option = QtGui.QStyleOptionButton()
if (index.flags() & QtCore.Qt.ItemIsEditable) > 0:
check_box_style_option.state |= QtGui.QStyle.State_Enabled
else:
check_box_style_option.state |= QtGui.QStyle.State_ReadOnly
if checked:
check_box_style_option.state |= QtGui.QStyle.State_On
else:
check_box_style_option.state |= QtGui.QStyle.State_Off
check_box_style_option.rect = self.getCheckBoxRect(option)
#if not index.model().hasFlag(index, Qt.ItemIsEditable):
#check_box_style_option.state |= QtGui.#QStyle.State_ReadOnly
check_box_style_option.state |= QtGui.QStyle.State_Enabled
QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_CheckBox, check_box_style_option, painter)
def editorEvent(self, event, model, option, index):
'''
Change the data in the model and the state of the checkbox
if the user presses the left mousebutton or presses
Key_Space or Key_Select and this cell is editable. Otherwise do nothing.
'''
if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0:
return False
# Do not change the checkbox-state
if event.type() == QtCore.QEvent.MouseButtonPress:
return False
if event.type() == QtCore.QEvent.MouseButtonRelease or event.type() == QtCore.QEvent.MouseButtonDblClick:
if event.button() != QtCore.Qt.LeftButton or not self.getCheckBoxRect(option).contains(event.pos()):
return False
if event.type() == QtCore.QEvent.MouseButtonDblClick:
return True
elif event.type() == QtCore.QEvent.KeyPress:
if event.key() != QtCore.Qt.Key_Space and event.key() != QtCore.Qt.Key_Select:
return False
else:
return False
# Change the checkbox-state
self.setModelData(None, model, index)
return True
def setModelData (self, editor, model, index):
'''
The user wanted to change the old state in the opposite.
'''
newValue = QtCore.Qt.Checked if not index.data() else QtCore.Qt.Unchecked
model.setData(index, newValue, QtCore.Qt.EditRole)
def getCheckBoxRect(self, option):
check_box_style_option = QtGui.QStyleOptionButton()
check_box_rect = QtGui.QApplication.style().subElementRect(QtGui.QStyle.SE_CheckBoxIndicator, check_box_style_option, None)
check_box_point = QtCore.QPoint (option.rect.x() +
option.rect.width() / 2 -
check_box_rect.width() / 2,
option.rect.y() +
option.rect.height() / 2 -
check_box_rect.height() / 2)
return QtCore.QRect(check_box_point, check_box_rect.size())
我还想添加另外两个功能
def setEnabled(editor)
def setDiabled(editor)
关键是我想将一些复选框设置为只读,并在选中另一个复选框时启用它们(这还不重要,我首先想创建一个只读复选框,其余的我将自己完成)
--编辑--
我考虑了以下解决方案
每次添加新行时,首先禁用该项目
def addRow(self):
...
...
self.item(row+1, 3).setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
在另一个功能中
def activate(self):
self.blockSignals(True)
...
...
user_readable = self.item(row, 2).data(QtCore.Qt.DisplayRole)
if user_readable == QtCore.Qt.Checked:
print user_readable
print self.item(row, 3).flags()
self.item(row, 3).setFlags(self.item(row, 3).flags() & QtCore.Qt.ItemIsEnabled)
else:
self.item(row, 3).setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
如果我理解正确,这里有一个简单的解决方案,我认为它可以满足您的需求。
class ReadOnlyCheck(QtGui.QCheckBox):
def __init__(self, parent=None, *args):
QtGui.QCheckBox.__init__(self, parent, *args)
def mousePressEvent(self, event):
event.ignore()
若要使该复选框显示为选中状态,请使用setChecked(True)
,以便在信号更改时连接到另一个复选框中的信号。
您可以使用setCellWidget
将这些输入到表格中。