Implementing QComboBox in a QTreeView item
-
Hi,
I am new to QT and currently working on a python GUI application using PySide.I have implemented a QStandardItemModel with my data and QTreeView to display it.
I would like to display some of the model indexes as a combobox with multiple selection items. the combobox needs to be always displayed (and not just while editing).So, after digging up I came up with the conclusion that I will need to implement a delegate (derived from --QStyledItemDelegate)-- that will render my desired indexes to display the Combobox with my data.
However I cannot seem to be able to actually display the combobox, and always end up with an empty column in my treeview.
This is a simplified version of my code:
@
class TreeViewDelegate(QtGui.QStyledItemDelegate):def init(self, parent):
QtGui.QStyledItemDelegate.init(self, parent)def paint(self, painter, option, index):
my_val = index.data(role=MyValRole)
if my_val is not None:
combo_opt = QtGui.QStyleOptionComboBox()
combo_opt.rect = option.rect
combo_opt.state = QtGui.QStyle.State_Enabled
combo_opt.frame = True
combo_opt.currentText = my_val
QtCore.QApplication.style().drawComplexControl(QtGui.Qstyle.CC_ComboBox, combo_opt, painter, QtGui.QTreview())else: QtGui.QStyledItemDelegate.paint(self, painter, option, index)
def sizeHint(self, option, index):
my_val = index.data(role=MyValRole)
nameFont = QtGui.QFont(option.font)
nameFM = QtGui.QFontMetrics(nameFont)
name_width = nameFM.width(my_val)
name_height = nameFM.height()return QtCore.QSize(name_width, name_height)
@What am I doing wrong here?
Any assistance will be appreciated.Thanks,
ynvb