PySide / PyQt QStyledItemDelegate list in table
I'm trying to create a table of lists in Python with Qt (PySide/PyQt - matters not) and my lists are squashed into the table cells.
Is there a way to get the list delegates to 'p开发者_C百科op out' of their cells? I've attached a simple code snippet - replace PySide
with PyQt4
depending on your preference
from PySide import QtCore, QtGui
class ListDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtGui.QListWidget(parent)
for i in range(12):
editor.addItem('list item %d' % i)
return editor
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
model = QtGui.QStandardItemModel(2, 2)
tableView = QtGui.QTableView()
delegate = ListDelegate()
tableView.setItemDelegate(delegate)
tableView.setModel(model)
for row in range(2):
for column in range(2):
item = QtGui.QStandardItem( 'None' )
model.setItem(row, column, item)
tableView.setWindowTitle('example')
tableView.show()
sys.exit(app.exec_())
So the answer is to use
QComboBox
rather than
QListWidget
so line 6 becomes
editor = QtGui.QComboBox(parent)
and all is right with the world. Hope this helps someone...
精彩评论