PDA

View Full Version : [PyQt] Selection dissapears when deselecting the delegate



enter
24th March 2014, 20:33
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.




class ComboBoxDelegate(QItemDelegate):
def __init__(self, owner, itemslist):
QItemDelegate.__init__(self, owner)
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
style = QApplication.style()
opt = QStyleOptionComboBox()
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 = QComboBox(parent)
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()
model.setData(index, QVariant(value))

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:
return QSize(20, 20)

enter
25th March 2014, 11:47
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.

enter
25th March 2014, 18:34
OK i finally found the solution : I needed to call the drawText method in paint.



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
style = QApplication.style()
opt = QStyleOptionComboBox()
#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))

lukeQt
26th October 2014, 22:59
Thank you!!! You saved me time.