Pyside - 委托readonly复选框

时间:2014-10-10 19:07:30

标签: python qt5 pyside qitemdelegate qcheckbox

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

1 个答案:

答案 0 :(得分: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将这些内容输入到表格中。