Results 1 to 2 of 2

Thread: PyQt - QTableView with comboBox

  1. #1
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default PyQt - QTableView with comboBox

    Hello,

    I want my QTableView to have a column of comboBoxes. After a lot of try-outs, I've reached my goal : display the comboBoxes.

    Now my problem is that they won't keep the selected item displayed.

    I use a custom delegate to do it, below is the relevant code :

    Qt Code:
    1. class ComboBoxDelegate(QtGui.QItemDelegate):
    2.  
    3. def __init__(self, owner, itemslist):
    4. QtGui.QItemDelegate.__init__(self, owner)
    5. self.itemslist = itemslist
    6.  
    7. def paint(self, painter, option, index):
    8. # Get Item Data
    9. value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
    10. # fill style options with item data
    11. style = QtGui.QApplication.style()
    12. opt = QtGui.QStyleOptionComboBox()
    13. opt.currentText = str(self.itemslist[value])
    14. opt.rect = option.rect
    15.  
    16. # draw item data as ComboBox
    17. style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter)
    18.  
    19. def createEditor(self, parent, option, index):
    20. # create the ProgressBar as our editor.
    21. editor = QtGui.QComboBox(parent)
    22. editor.addItems(self.itemslist)
    23. editor.setCurrentIndex(0)
    24. editor.installEventFilter(self)
    25. return editor
    26.  
    27. def setEditorData(self, editor, index):
    28. value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
    29. editor.setCurrentIndex(value)
    30.  
    31. def setModelData(self,editor,model,index):
    32. value = editor.currentIndex()
    33. model.setData(index, QtCore.QVariant(value))
    34.  
    35. def updateEditorGeometry(self, editor, option, index):
    36. editor.setGeometry(option.rect)
    37.  
    38.  
    39.  
    40. class SequenceGridModel(QtCore.QAbstractTableModel):
    41. def __init__(self, datain, headerdata, parent=None, *args):
    42. QtCore.QAbstractTableModel.__init__(self, parent, *args)
    43. self.parent = parent
    44. self.arraydata = datain
    45. self.headerdata = headerdata
    46.  
    47. def flags(self, index):
    48. if index.column() == 13:
    49. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
    50. return QtCore.QAbstractTableModel.flags(self, index)
    51.  
    52.  
    53. def data(self, index, role = QtCore.Qt.DisplayRole):
    54. if not index.isValid():
    55. if role == QtCore.Qt.UserRole:
    56. return None
    57. else:
    58. return QtCore.QVariant()
    59.  
    60. value = QtCore.QVariant(self.arraydata[index.row()][index.column()])
    61.  
    62. if QtCore.Qt.UserRole == role:
    63. return value
    64. elif role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
    65. return QtCore.QVariant(value)
    66. return QtCore.QVariant()
    67.  
    68.  
    69.  
    70. class SequenceGrid(QtGui.QTableView):
    71. def __init__(self, parent=None):
    72. QtGui.QTableView.__init__(self, parent)
    73.  
    74. self.selectedIndexes = []
    75. self.parent = parent
    76.  
    77. self.checkValues = ['TODO', 'WAITING', 'RETAKE', 'OK']
    78.  
    79. self.model = SequenceGridModel(self.data, header, self)
    80. self.setModel(self.model)
    81. self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
    82. self.viewport().installEventFilter(self)
    83. self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues))
    84.  
    85. self.setColumnWidth(13, 64)
    To copy to clipboard, switch view to plain text mode 


    Thanks for your attention !

  2. #2
    Join Date
    May 2011
    Location
    Paris
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: PyQt - QTableView with comboBox

    OK, figured out the solution. I called the QAbstractItemView.openPersistentEditor(index) function in the paint method of the delegate and it works for me.

    Hope it can help out someone

Similar Threads

  1. How to populate Combobox in QTableView
    By ronak.vashi in forum Newbie
    Replies: 8
    Last Post: 4th December 2010, 17:09
  2. Replies: 1
    Last Post: 4th August 2010, 19:50
  3. Upgrading from PyQt 4.5 to PyQt 4.7
    By RogerCon in forum Installation and Deployment
    Replies: 0
    Last Post: 14th July 2010, 18:52
  4. Help ComboBox
    By vinny gracindo in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 19:41
  5. ComboBox of bmp:s
    By SailinShoes in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2008, 15:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.