Python 禁用的qtablewidgetitem不显示为灰色
我有一个Python 禁用的qtablewidgetitem不显示为灰色,python,qt,pyqt,qtablewidgetitem,Python,Qt,Pyqt,Qtablewidgetitem,我有一个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.
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
|
是按位或。它所做的是打开启用标志,而不管其原始状态如何。
^
将对其进行切换
如果您想关闭国旗,不管它的原始状态如何,只需使用它的赞美语(~)对其进行(&),如下所示:
您可以对要关闭或打开的任何标志应用这些原则,如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)
my_checkbox_item = self.cellWidget(row+1, self.columns["USER_ACCESS"])
my_checkbox_item.setEnabled(False)
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
有关更多详细信息,请查看此链接:
另一个涉及此主题的精彩答案(非常有用):
更新-1:
如果您的单元格中有小部件形式的项(egs.QCheckBox
),您可能希望以不同的方式处理它。您可能希望禁用相应的小部件。因此,在您的情况下,您可以执行以下操作:
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)
my_checkbox_item = self.cellWidget(row+1, self.columns["USER_ACCESS"])
my_checkbox_item.setEnabled(False)
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
更新-2:
现在,您已经用更多代码更新了问题,下面是另一个更新:
在paint
方法中,必须应用与本答案第一部分所示相同的位运算原理。因此,您必须执行以下操作:
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)
my_checkbox_item = self.cellWidget(row+1, self.columns["USER_ACCESS"])
my_checkbox_item.setEnabled(False)
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
这应该可以解决问题。你为什么不用正常的方法…?或者用分号?看起来您还将其设置为“对我启用”。@kiss-o-matic,Python不需要使用分号换行。我错过了PyQt标记。;)是 啊你发表评论时,他们不在那里。我最近添加了它们:)这非常有用,但它仍然显示为白色而不是灰色!我不能点击它或检查它,这很奇怪。你用的是哪种操作系统?事实上我想我看到了一个问题。我会用可能的解决方案更新我的答案。你能分享一下你是如何设置每个项目的吗?只需设置每个项目的代码。您是否使用
setCellWidget()
将QCheckBox
设置为单元格?我更新了我的帖子,并添加了复选框授权人的完整代码