How to use a custom widget as an ItemDelegate
-
Hi Everyone,
I am trying to provide a custom editor for a list of string items. Imagine each string as having several elements separated by a :
"ab*c"I want to display them as such, but when you edit an item, depending on the value of the first, I want to display a widget in place to edit the element. This could be a drop down for the second item "b" and a text edit for the third item "c".
I am able to use a QComboBox as a delegate, but when I assemble a widget by placing two QComboBox side by side, funny things happen:
On the first double click, the item is highlighted light grey, if I click on it again it vanishes.Here is my PySide/Python code so far ... any insight would be appreciated.
@
from PySide import QtGui, QtCore
from PySide.QtGui import QStringListModel, QListWidget, QItemDelegate, QLabel, QComboBox, QWidgetclass LinkEditor(QWidget):
def init(self, parent = None):
super(LinkEditor, self).init(parent)
layout = QtGui.QHBoxLayout(self)
c1 = QComboBox(parent)
c1.addItem('spam')
c1.addItem('ham')
c1.addItem('kumquat')
layout.addWidget(c1)
c2 = QComboBox(parent)
c2.addItem('eat')
c2.addItem('spill')
layout.addWidget(c2)
self.setLayout(layout)class LinkElementDelegate(QItemDelegate):
def init(self, parent = None):
super(LinkElementDelegate, self).init(parent)def createEditor(self, parent, option, index): l = LinkEditor(parent) return l
class LinkListView(QtGui.QListView):
def init(self, data = None, parent=None):
super(LinkListView, self).init(parent)
self.setAcceptDrops(True)
self.data = data
self.delegate = LinkElementDelegate(self)
self.setItemDelegate(self.delegate )class LinkMain(QtGui.QWidget):
def init(self, et = None, main = None, parent=None):
super(LinkMain, self).init(parent)
self.et = et
self.main = main
self.links = ['hameat', 'spamspill', 'ham*spill']
self.model = QStringListModel(self.links)
layout = QtGui.QVBoxLayout(self)
layout.setContentsMargins(1,1,1,1)
link_list = LinkListView(data = self.links, parent = self)
link_list.setModel(self.model)
self.setAcceptDrops(True);
layout.addWidget(link_list)
self.setLayout(layout)
self.show()if name == 'main':
import sys
app = QtGui.QApplication(sys.argv)
form = LinkMain()
form.show()
app.exec_()
@EDIT: moved to Language Bindings, Gerolf