[PyQt] Selection dissapears when deselecting the delegate
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.
Code:
def __init__(self, owner, itemslist):
self.itemslist = itemslist
def paint(self, painter, option, index):
# Check if we're on top level of tree
leftmostSibling = index.sibling(index.row(), 0)
if leftmostSibling.parent().isValid():
# We're not on top level of tree
value = index.data(Qt.DisplayRole)
print("paint" + value)
# Fill style options with item data
opt.currentText = self.itemslist.stringList()[0]
opt.rect = option.rect
# Draw item data as ComboBox
style.
drawComplexControl(QStyle.
CC_ComboBox, opt, painter
)
def createEditor(self, parent, option, index):
# Check if we're on top level of tree
leftmostSibling = index.sibling(index.row(), 0)
if leftmostSibling.parent().isValid():
# We're not on top level of tree
value = index.data(Qt.DisplayRole)
# create the QStyleOptionComboBoxBox as our editor.
editor.setModel(self.itemslist)
if value == int:
editor.setCurrentIndex(value)
editor.installEventFilter(self)
return editor
def setEditorData(self, editor, index):
value = index.data(Qt.DisplayRole)
editor.setCurrentIndex(value)
def setModelData(self, editor, model, index):
value = editor.currentIndex()
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def sizeHint(self, option, index):
# Overload sizeHint method so that the QComboBox isn't shrinked
# by the QTreeView row height
index_data = index.data(Qt.SizeHintRole)
if index_data is None:
Re: [PyQt] Selection dissapears when deselecting the delegate
To be more specific, when I change the index of the ComboBox it shows the index label until I deselect the ComboBox. Then there is no label.
Re: [PyQt] Selection dissapears when deselecting the delegate
OK i finally found the solution : I needed to call the drawText method in paint.
Code:
def paint(self, painter, option, index):
# Check if we're on top level of tree
leftmostSibling = index.sibling(index.row(), 0)
if leftmostSibling.parent().isValid():
# We're not on top level of tree
value = index.data(Qt.DisplayRole)
print(value)
# Fill style options with item data
#opt.currentText = str(self.itemslist.stringList[0])
opt.rect = option.rect
# Draw item data as ComboBox
style.
drawComplexControl(QStyle.
CC_ComboBox, opt, painter
) painter.drawText(option.rect, Qt.AlignVCenter, str(value))
Re: [PyQt] Selection dissapears when deselecting the delegate
Thank you!!! You saved me time.