Note that there are some explanatory texts on larger screens.

plurals
  1. POselected item of comboBox in custom Delegate from QTableView
    primarykey
    data
    text
    <p>I use a custom delegate to display a column of comboBoxes in my QTableView. The values are the same for all the comboBoxes so it's not really the population part that gives me trouble. </p> <p>I want them to show as the selected item, some value that I can retrieve from a database. I have access to the database from the delegate, but in order to send my request, I need the row of the comboBox.</p> <p>So I guess my question is : how can you iterate over all the rows of the table and do some action from inside the custom delegate ?</p> <p>If it can help here is my custom delegate class :</p> <pre><code>class ComboBoxDelegate(QtGui.QItemDelegate): def __init__(self, parent, itemslist): QtGui.QItemDelegate.__init__(self, parent) self.itemslist = itemslist self.parent = parent def paint(self, painter, option, index): # Get Item Data value = index.data(QtCore.Qt.DisplayRole).toInt()[0] # value = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]] # fill style options with item data style = QtGui.QApplication.style() opt = QtGui.QStyleOptionComboBox() opt.currentText = str(self.itemslist[value]) opt.rect = option.rect # draw item data as ComboBox style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter) self.parent.openPersistentEditor(index) def createEditor(self, parent, option, index): ##get the "check" value of the row # for row in range(self.parent.model.rowCount(self.parent)): # print row self.editor = QtGui.QComboBox(parent) self.editor.addItems(self.itemslist) self.editor.setCurrentIndex(0) self.editor.installEventFilter(self) self.connect(self.editor, QtCore.SIGNAL("currentIndexChanged(int)"), self.editorChanged) return self.editor # def setEditorData(self, editor, index): # value = index.data(QtCore.Qt.DisplayRole).toInt()[0] # editor.setCurrentIndex(value) def setEditorData(self, editor, index): text = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]] pos = self.editor.findText(text) if pos == -1: pos = 0 self.editor.setCurrentIndex(pos) def setModelData(self,editor,model,index): value = self.editor.currentIndex() model.setData(index, QtCore.QVariant(value)) def updateEditorGeometry(self, editor, option, index): self.editor.setGeometry(option.rect) def editorChanged(self, index): check = self.editor.itemText(index) id_seq = self.parent.selectedIndexes[0][0] update.updateCheckSeq(self.parent.db, id_seq, check) </code></pre> <p>And I call it fromthe QTableView like this : </p> <pre><code>self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged) self.viewport().installEventFilter(self) self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues)) </code></pre> <p>Hope I was clear enough, thanks for your attention</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload