Pergunta

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 'pop 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_())
Foi útil?

Solução

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...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top