0

我想将只读复选框委托给 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)
4

1 回答 1

0

如果我理解正确,这是一个简单的解决方案,我认为它可以满足您的需求。

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

于 2014-10-10T20:30:28.523 回答