Relevant code snippets. These aren't the complete methods, just those parts involving the check state (that's probably obvious):

The tree model:

Qt Code:
  1. class TreeModel (QAbstractItemModel):
  2.  
  3. def flags(self, index):
  4. flags = Qt.ItemIsEnabled
  5. item = self.get_item(index)
  6. # ...
  7. if index.column() is TreeModel.COLUMN_X:
  8. if item.is_active():
  9. flags |= Qt.ItemIsSelectable | Qt.ItemIsUserCheckable
  10. return flags
  11.  
  12. def data(self, index, role):
  13. data = QVariant()
  14. item = self.get_item(index)
  15. # ...
  16. if role == Qt.CheckStateRole:
  17. if index.column() is TreeModel.COLUMN_X:
  18. if item.is_active():
  19. data = QVariant(Qt.Checked)
  20. else:
  21. data = QVariant(Qt.Unchecked)
  22. return data
  23.  
  24. def setData(self, index, value, role):
  25. status = False
  26. item = self.get_item(index)
  27. # ...
  28. if role == Qt.CheckStateRole:
  29. if index.column() is TreeModel.COLUMN_X:
  30. if not item.is_active():
  31. item.set_active(True)
  32. else:
  33. item.set_active(False)
  34. status = True
  35. self.emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)', index, index)
  36. 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.



Qt Code:
  1. class TreeRenderer(QItemDelegate):
  2.  
  3. def drawCheck( self, painter, option, rect, state ):
  4. '''Draw a radio button instead of a check button for mutually exclusive fields.'''
  5. if not rect.isValid(): return
  6. option.rect = rect
  7. option.state &= ~QStyle.State_HasFocus
  8. option.state |= {
  9. Qt.Unchecked: QStyle.State_Off,
  10. Qt.PartiallyChecked: QStyle.State_NoChange,
  11. Qt.Checked: QStyle.State_On
  12. }[ state ]
  13. style = self.view.style() if self.view else QApplication.style()
  14. style.drawPrimitive( QStyle.PE_IndicatorRadioButton, option, painter, self.view )
To copy to clipboard, switch view to plain text mode