Update on earlier post - custom widget as an QItemDelegate
-
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 should not be a plain single value widget (which works fine in my example below), but a combination of several widgets to edit several parts of the field in parallel, such as two QComboBox side by side (or a text editor with autocomplete, and two spin boxes or some such thing).
I am able to use a single QComboBox as a delegate.
I tried five different ways (not counting many other failed attempts).
The first one, with a QComboBox, works fine - LinkEditorCombo delegate in my example below.
The second one, with a QWidget containing a single QComboBox, works fine - LinkEditorCombo2 delegate in my example below.
The third one, with a QWidget containing a single QComboBox, only renders the second combo box - LinkEditorCombo3 delegate in my example below.
The fourth one, with a QWidget containing a single QComboBox, does not render a delegate editor at all -LinkEditorCombo4 delegate in my example below.
The fifth one, with a QWidget containing a single QComboBox, also does not render a delegate editor at all - LinkEditorCombo5 delegate in my example below.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, QWidget, QStyledItemDelegateclass LinkEditorCombo(QComboBox):
def init(self, parent = None):
super(LinkEditorCombo, self).init(parent)
self.addItem('spam')
self.addItem('ham')
self.addItem('kumquat')
def setText(self, text):
pass
def text(self):
return 'spam'class LinkEditorCombo2(QWidget):
def init(self, parent = None):
super(LinkEditorCombo2, self).init(parent)
self.c1 = QComboBox(self)
self.c1.addItem('spam2')
self.c1.addItem('ham2')
self.c1.addItem('kumquat2')
def setText(self, text):
pass
def text(self):
return 'spam'class LinkEditorCombo3(QWidget):
def init(self, parent = None):
super(LinkEditorCombo3, self).init(parent)
self.c1 = QComboBox(self)
self.c1.addItem('spam3')
self.c1.addItem('ham3')
self.c1.addItem('kumquat3')
self.c2 = QComboBox(self)
self.c2.addItem('tea3')
self.c2.addItem('espresso3')
self.c2.addItem('champagne3')
def setText(self, text):
pass
def text(self):
return 'spam'class LinkEditorCombo4(QWidget):
def init(self, parent = None):
super(LinkEditorCombo4, self).init(parent)
self.layout = QtGui.QHBoxLayout(self)
self.c1 = QComboBox(self)
self.c1.addItem('ham4')
self.c1.addItem('spam4')
self.c1.addItem('kumquat4')
self.layout.addWidget(self.c1)
self.setLayout(self.layout)
def setText(self, text):
pass
def text(self):
return 'spam'class LinkEditorCombo5(QWidget):
def init(self, parent = None):
super(LinkEditorCombo5, self).init(parent)
self.layout = QtGui.QHBoxLayout(self)
self.c1 = QComboBox(self)
self.c1.addItem('ham5')
self.c1.addItem('spam5')
self.c1.addItem('kumquat5')
self.c2 = QComboBox(self)
self.c2.addItem('tea4')
self.c2.addItem('espresso5')
self.c2.addItem('champagne!5')
self.layout.addWidget(self.c1)
self.layout.addWidget(self.c2)
self.setLayout(self.layout)
def setText(self, text):
pass
def text(self):
return 'spam'class LinkElementDelegate(QStyledItemDelegate):
def init(self, parent = None):
super(LinkElementDelegate, self).init(parent)def createEditor(self, parent, option, index): editor = LinkEditorCombo5(parent) print 'createEditor' return editor def setEditorData(self, editor, index): text = index.model().data(index, QtCore.Qt.DisplayRole) print 'seteditordata "' + text + '"' editor.setText(text) def setModelData(self, editor, model, index): print 'setmodeldata"' + editor.text() + '"' model.setData(index, editor.text())
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, parent=None):
super(LinkMain, self).init(parent)
self.links = ['hameat', 'spamspill', 'ham*spill']
self.model = QStringListModel(self.links)
layout = QtGui.QVBoxLayout(self)
link_list = LinkListView(data = self.links, parent = self)
link_list.setModel(self.model)
layout.addWidget(link_list)
#layout.addWidget(LinkEditorCombo(self))
#layout.addWidget(LinkEditorCombo2(self))
#layout.addWidget(LinkEditorCombo3(self))
layout.addWidget(LinkEditorCombo4(self))
self.setLayout(layout)if name == 'main':
import sys
app = QtGui.QApplication(sys.argv)
form = LinkMain()
form.show()
app.exec_()@