如何在QTableVi的QComboBox中添加项目

2024-10-01 22:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在QComboBox中添加项目时遇到问题。如果可能,谁能告诉我如何使用下面的代码添加项目?在

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)

这是我目前的输出,但我想在我的组合框中添加某些项。在

output


Tags: rectselfdataindexvaluestyledefcheck
1条回答
网友
1楼 · 发布于 2024-10-01 22:27:34

我已经更正了你的密码。当子类创建编辑器时,必须在组合框中添加项,而不是在绘制中添加项。我只发布编辑过的代码(其他代码部分是正确的):

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)

相关问题 更多 >

    热门问题