Skip to content

Instantly share code, notes, and snippets.

@rsgalloway
Created September 27, 2013 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rsgalloway/6733137 to your computer and use it in GitHub Desktop.
Save rsgalloway/6733137 to your computer and use it in GitHub Desktop.
class QCheckableHeaderView(QtGui.QHeaderView):
'''
Checkable QHeaderView. Column 0 contains a checkbox that emits
a signal when it's check state is updated.
'''
is_on = True
signal_checked = QtCore.pyqtSignal(bool)
def __init__(self, *args, **kwargs):
super(QCheckableHeaderView, self).__init__(*args, **kwargs)
self.setStretchLastSection(True)
self.setDefaultAlignment(QtCore.Qt.AlignLeft)
def paintSection(self, painter, rect, logicalindex):
painter.save()
super(QCheckableHeaderView, self).paintSection(painter, rect, logicalindex)
painter.restore()
if logicalindex == 0:
option = QtGui.QStyleOptionButton()
option.rect = QtCore.QRect(0, 7, 5, 0)
if self.is_on:
option.state = QtGui.QStyle.State_On
else:
option.state = QtGui.QStyle.State_Off
self.style().drawControl(QtGui.QStyle.CE_CheckBox, option, painter)
def mousePressEvent(self, event):
logicalindex = self.logicalIndexAt(event.pos())
if logicalindex == 0:
self.is_on = not self.is_on
self.updateSection(0)
self.signal_checked.emit(self.is_on)
super(QCheckableHeaderView, self).mousePressEvent(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment