Created
September 27, 2013 18:36
-
-
Save rsgalloway/6733137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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