I'm late to the party - it looks like you have a solution that works. For future reference, another way you can do this is to use mouse events that save all your signals in the way they should:
from PyQt4 import QtGui, QtCore class MyCheckBox(QtGui.QCheckBox): def __init__( self, *args ): super(MyCheckBox, self).__init__(*args) # will fail if passing **kwargs self._readOnly = False def isReadOnly( self ): return self._readOnly def mousePressEvent( self, event ): if ( self.isReadOnly() ): event.accept() else: super(MyCheckBox, self).mousePressEvent(event) def mouseMoveEvent( self, event ): if ( self.isReadOnly() ): event.accept() else: super(MyCheckBox, self).mouseMoveEvent(event) def mouseReleaseEvent( self, event ): if ( self.isReadOnly() ): event.accept() else: super(MyCheckBox, self).mouseReleaseEvent(event) # Handle event in which the widget has focus and the spacebar is pressed. def keyPressEvent( self, event ): if ( self.isReadOnly() ): event.accept() else: super(MyCheckBox, self).keyPressEvent(event) @QtCore.pyqtSlot(bool) def setReadOnly( self, state ): self._readOnly = state readOnly = QtCore.pyqtProperty(bool, isReadOnly, setReadOnly)
Setting up the code this way gives you a few things (which you may or may not need to take care of), but can be useful when developing your own Qt widgets:
- Event consumption blocks signal emission, so you can connect other slots to things like clicking and switching. If you are looking for these signals, and then just toggle the value on / off, then other widgets that listen to these signals will not start correctly.
- Using isReadOnly / setReadOnly supports a class that matches the Qt style
- Creating pyqtSignals and pyqtSlots will help if you open the plugin for Qt Designer
source share