禁用qtablewidgetitem未显示为灰色

时间:2014-12-18 14:01:44

标签: 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.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

1 个答案:

答案 0 :(得分:3)

改为使用^

flags ^= QtCore.Qt.ItemIsEnabled

|是按位OR。它的作用是将启用标志置ON,而不管其原始状态如何。 ^会切换它。

如果你想要关闭其原始状态的标志REGARDLESS,只需和它的COMPLIMENT(〜)一样(&),就像这样:

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)

<强> UPDATE-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

那应该解决它。