我有一个qtablewidgetitem
,里面有一个QCheckbox
禁用qtablewigetitem
时如下
flags = self.item(row+1, self.columns["USER_ACCESS"]).flags()
flags |= QtCore.Qt.ItemIsSelectable
flags |= QtCore.Qt.ItemIsEditable
flags |= QtCore.Qt.ItemIsEnabled
self.item(row+1, self.columns["USER_ACCESS"]).setFlags(flags)
它已被禁用,我无法单击它,但它会显示,因为它仍然处于启用状态。
我想将其显示为灰色
更新:
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)
self.parent.sort()
self.parent.sort()
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())
这是我如何将其放入 QTableWidgetItem
def delegate(self, column, delegater):
self.setItemDelegateForColumn(column, delegater)
pass
最佳答案
使用 ^
代替。
flags ^= QtCore.Qt.ItemIsEnabled
|
是按位或。它的作用是将启用标志打开,无论其原始状态如何。
^
将切换它。
如果您想关闭该标志,无论其原始状态如何,只需将其与其补充 (~) 进行 AND (&) 操作,如下所示:
flags = flags & ~QtCore.Qt.ItemIsEnabled
您可以将这些原则应用于您想要关闭或打开的任何标志,例如 QtCore.Qt.ItemIsSelectable
等。
在您的情况下,代码将类似于:
flags = self.item(row+1, self.columns["USER_ACCESS"]).flags()
flags &= ~QtCore.Qt.ItemIsSelectable
flags &= ~QtCore.Qt.ItemIsEditable
flags &= ~QtCore.Qt.ItemIsEnabled
self.item(row+1, self.columns["USER_ACCESS"]).setFlags(flags)
查看此内容了解更多详细信息: https://wiki.python.org/moin/BitwiseOperators
另一个涉及该主题的精彩答案(非常有用): How to find specific Qt.ItemFlag occurrence into custom Qt.ItemFlags instance in PyQt?
UPDATE-1:
如果您的单元格具有 Widgets 形式的项目(例如 QCheckBox
),您可能需要以不同的方式处理它。您可能想禁用相应的小部件。因此,在您的情况下,您会执行以下操作:
my_checkbox_item = self.cellWidget(row+1, self.columns["USER_ACCESS"])
my_checkbox_item.setEnabled(False)
更新2: 因为,您现在已经用更多代码更新了您的问题,这是另一个更新: 在您的 Paint 方法中,您必须应用与本答案第一部分所示相同的按位运算原理。所以你必须做类似的事情:
if (index.flags() & QtCore.Qt.ItemIsEditable) > 0:
check_box_style_option.state |= QtGui.QStyle.State_Enabled
check_box_style_option.state &= ~QtGui.QStyle.State_ReadOnly
else:
check_box_style_option.state &= ~QtGui.QStyle.State_Enabled
check_box_style_option.state |= QtGui.QStyle.State_ReadOnly
..并删除这些行:
#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
这应该可以解决问题。
关于python - 禁用的 qtablewidgetitem 不会显示为灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548336/