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:
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)
To copy to clipboard, switch view to plain text mode
Bookmarks