2

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.

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.

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 ?

If it can help here is my custom delegate class :

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)

And I call it fromthe QTableView like this :

self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
self.viewport().installEventFilter(self)
self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues))

Hope I was clear enough, thanks for your attention

Johanna
  • 1,343
  • 4
  • 25
  • 44

1 Answers1

1

Not sure if accessing the database from the delegate is a right thing to do. Your delegate can contain reference to the instance of QAbstractTableModel which the QTableView refers to. You can then use methods in the model to iterate over rows of the table.

sateesh
  • 27,947
  • 7
  • 36
  • 45
  • Well I managed to display the correct selected item in the compboBoxes. But as you stated I'm accessing the database from the delegate, but I don't really see how to implement what you suggested. And I still have a problem : the comboBoxes don't "follow" when I reorder the tableView.. – Johanna May 12 '11 at 10:17
  • Is it possible to call a repaint of the delegate from the model ? – Johanna May 12 '11 at 12:15
  • but how would the model know about the delegate, ok you can pass delegate reference to the model. Delegates are their to improve the presentation (i.e. better view) making the model and delegate interdependent would make your code brittle. – sateesh May 12 '11 at 13:37
  • Well for now I don't have an instance of the delegate in the model. Actually I have an instance of the model and one of the delegate on the viex, so I have "inter-access" but they're not really interdependant. I tried to update the delegate from almost everywhere in my code, nothing seem to work.. – Johanna May 12 '11 at 14:19