Angelica Lim
Angelica Lim

Reputation: 61

How to add items in QComboBox inside QTableView

I'm having problems adding items in my QComboBox. if possible can anyone tell me how to add items using the code below?

class ComboBoxDelegate(QtGui.QItemDelegate):

    def __init__(self, owner, itemslist):
        QtGui.QItemDelegate.__init__(self, owner)
        self.itemslist = itemslist

    def paint(self, painter, option, index):        
        # Get Item Data
        value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
        #print value
        # 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)

    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 setModelData(self,editor,model,index):
        value = editor.currentIndex()
        model.setData(index, QtCore.QVariant(value))

    def updateEditorGeometry(self, editor, option, index):
        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)

this is my output as of now, but i want to add certain items in my combobox.

output

Upvotes: 1

Views: 1856

Answers (1)

J03Bukowski
J03Bukowski

Reputation: 13

I have corrected your code. You must add items in combobox when subclass create editor and not in paint. I post only edited code (other code part it's correct):

class ComboBoxDelegate(QtGui.QItemDelegate):

    def __init__(self, owner, itemlist):
        QtGui.QItemDelegate.__init__(self, owner)
        self.itemslist = itemlist

    def createEditor(self, parent, option, index): 
        self.editor = QtGui.QComboBox(parent)
        for i in range(0, len(self.itemslist)):
            self.editor.addItem(str(self.itemslist[i]))

        self.editor.installEventFilter(self)    
        self.connect(self.editor, QtCore.SIGNAL("currentIndexChanged(int)"), self.editorChanged)

        return self.editor


    def paint(self, painter, option, index):        

        value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
        opt = QtGui.QStyleOptionComboBox()
        opt.text = str(self.itemslist[value])
        opt.rect = option.rect

        QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ItemViewItem, opt, painter)

Upvotes: 0

Related Questions