0

I want to delegate a readonly Checkbox into a QTableWidget

I have the following class for the a checkbox which is shown enabled (editable)

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())

I also want to add two other functions

def setEnabled(editor)
def setDiabled(editor)

The point is I want to set some checkboxes read-only and enable them when another checkbox is checked (this is not important yet, i first want to create a read-only checkbox, the rest i will do it by myself)

-- EDIT --

I thought about the following solution

first disable that item each time a new row is added

def addRow(self):
        ...
        ...
        self.item(row+1, 3).setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)

in another function

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)
Elteroooo
  • 2,913
  • 3
  • 33
  • 40

1 Answers1

0

Here is a simple solution that I think does what you are after, if I understood correctly.

class ReadOnlyCheck(QtGui.QCheckBox):
    def __init__(self, parent=None, *args):
        QtGui.QCheckBox.__init__(self, parent, *args)

    def mousePressEvent(self, event):
        event.ignore()

To make the box appear selected, you use setChecked(True), so you can connect to the signal from your other check-box, when it changes.

You can enter these into the table with setCellWidget.

mdurant
  • 27,272
  • 5
  • 45
  • 74
  • in the event, you can add modifiers to call the usual QtGui.QCheckBox.mousePressEvent in case you want to pass the click through. – mdurant Oct 10 '14 at 20:32
  • i thought i might use setFlags to make the QTableWidgetitem not editable, then re-enable it when another checkbox was checked. but i'm now facing a problem how to re-enable it after beeing disabling it! see edit – Elteroooo Oct 10 '14 at 20:36
  • I don't see where you set the flags, only where you change the display style. This doesn't work? Will you try my (apparently) simple solution, or stick with the delegate route? – mdurant Oct 10 '14 at 20:40
  • It's a little complicated - I advise you to check whether those sets of flags produce the behaviour you're after, and then whether the code that's supposed to set them is called at the right time. – mdurant Oct 10 '14 at 20:45