Hi,

I have a problem with my QComboBox delegate. The selection of the ComboBox dissapears when I deselect the delegate. I pasted the delegate code below. Did I forget to overload a QItemDelegate method ?

I heard about an openPersistantEditor method but I don't know where to put it in my code.

What am I missing ?

Thanks for your help.


Qt Code:
  1. class ComboBoxDelegate(QItemDelegate):
  2. def __init__(self, owner, itemslist):
  3. QItemDelegate.__init__(self, owner)
  4. self.itemslist = itemslist
  5.  
  6. def paint(self, painter, option, index):
  7. # Check if we're on top level of tree
  8. leftmostSibling = index.sibling(index.row(), 0)
  9. if leftmostSibling.parent().isValid():
  10. # We're not on top level of tree
  11. value = index.data(Qt.DisplayRole)
  12. print("paint" + value)
  13.  
  14. # Fill style options with item data
  15. style = QApplication.style()
  16. opt.currentText = self.itemslist.stringList()[0]
  17. opt.rect = option.rect
  18.  
  19. # Draw item data as ComboBox
  20. style.drawComplexControl(QStyle.CC_ComboBox, opt, painter)
  21.  
  22. def createEditor(self, parent, option, index):
  23. # Check if we're on top level of tree
  24. leftmostSibling = index.sibling(index.row(), 0)
  25. if leftmostSibling.parent().isValid():
  26. # We're not on top level of tree
  27. value = index.data(Qt.DisplayRole)
  28. # create the QStyleOptionComboBoxBox as our editor.
  29. editor = QComboBox(parent)
  30. editor.setModel(self.itemslist)
  31. if value == int:
  32. editor.setCurrentIndex(value)
  33. editor.installEventFilter(self)
  34. return editor
  35.  
  36. def setEditorData(self, editor, index):
  37. value = index.data(Qt.DisplayRole)
  38. editor.setCurrentIndex(value)
  39.  
  40. def setModelData(self, editor, model, index):
  41. value = editor.currentIndex()
  42. model.setData(index, QVariant(value))
  43.  
  44. def updateEditorGeometry(self, editor, option, index):
  45. editor.setGeometry(option.rect)
  46.  
  47. def sizeHint(self, option, index):
  48. # Overload sizeHint method so that the QComboBox isn't shrinked
  49. # by the QTreeView row height
  50. index_data = index.data(Qt.SizeHintRole)
  51. if index_data is None:
  52. return QSize(20, 20)
To copy to clipboard, switch view to plain text mode