2012-11-20 66 views
1

我在QComboBox中添加項目時遇到問題。如果可能的話,任何人都可以告訴我如何使用下面的代碼添加項目?如何在QComboBox中添加項目在QTableView中

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

回答

0

我已經糾正你的代碼。當子類創建編輯器而不是繪製時,您必須在組合框中添加項目。我只發佈編輯的代碼(其他代碼部分是正確的):

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)