Relevant code snippets. These aren't the complete methods, just those parts involving the check state (that's probably obvious):
The tree model:
def flags(self, index):
flags = Qt.ItemIsEnabled
item = self.get_item(index)
# ...
if index.column() is TreeModel.COLUMN_X:
if item.is_active():
flags |= Qt.ItemIsSelectable | Qt.ItemIsUserCheckable
return flags
def data(self, index, role):
item = self.get_item(index)
# ...
if role == Qt.CheckStateRole:
if index.column() is TreeModel.COLUMN_X:
if item.is_active():
else:
return data
def setData(self, index, value, role):
status = False
item = self.get_item(index)
# ...
if role == Qt.CheckStateRole:
if index.column() is TreeModel.COLUMN_X:
if not item.is_active():
item.set_active(True)
else:
item.set_active(False)
status = True
self.emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)', index, index)
return status
class TreeModel (QAbstractItemModel):
def flags(self, index):
flags = Qt.ItemIsEnabled
item = self.get_item(index)
# ...
if index.column() is TreeModel.COLUMN_X:
if item.is_active():
flags |= Qt.ItemIsSelectable | Qt.ItemIsUserCheckable
return flags
def data(self, index, role):
data = QVariant()
item = self.get_item(index)
# ...
if role == Qt.CheckStateRole:
if index.column() is TreeModel.COLUMN_X:
if item.is_active():
data = QVariant(Qt.Checked)
else:
data = QVariant(Qt.Unchecked)
return data
def setData(self, index, value, role):
status = False
item = self.get_item(index)
# ...
if role == Qt.CheckStateRole:
if index.column() is TreeModel.COLUMN_X:
if not item.is_active():
item.set_active(True)
else:
item.set_active(False)
status = True
self.emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)', index, index)
return status
To copy to clipboard, switch view to plain text mode
The item delegate. Compare to the implementation of QItemDelegate.drawCheck. It differs only in two places:
- The last line uses QStyle.PE_IndicatorRadioButton instead of QStyle.PE_IndicatorViewItemCheck
- In the last two lines Instead of asking the private implementation for the owning widget (which can't be done), assume it's the tree view.
def drawCheck( self, painter, option, rect, state ):
'''Draw a radio button instead of a check button for mutually exclusive fields.'''
if not rect.isValid(): return
option.rect = rect
option.
state &= ~
QStyle.
State_HasFocus option.state |= {
Qt.
Unchecked: QStyle.
State_Off,
Qt.
PartiallyChecked: QStyle.
State_NoChange,
}[ state ]
style
= self.
view.
style() if self.
view else QApplication.
style() style.
drawPrimitive( QStyle.
PE_IndicatorRadioButton, option, painter, self.
view )
class TreeRenderer(QItemDelegate):
def drawCheck( self, painter, option, rect, state ):
'''Draw a radio button instead of a check button for mutually exclusive fields.'''
if not rect.isValid(): return
option.rect = rect
option.state &= ~QStyle.State_HasFocus
option.state |= {
Qt.Unchecked: QStyle.State_Off,
Qt.PartiallyChecked: QStyle.State_NoChange,
Qt.Checked: QStyle.State_On
}[ state ]
style = self.view.style() if self.view else QApplication.style()
style.drawPrimitive( QStyle.PE_IndicatorRadioButton, option, painter, self.view )
To copy to clipboard, switch view to plain text mode
Bookmarks