从QTableView自定义委托中选择的comboBox项

时间:2011-05-10 08:56:51

标签: combobox pyqt qtableview qitemdelegate

我使用自定义委托在我的QTableView中显示一列comboBox。 所有组合框的值都是相同的,因此不是真正的人口部分给我带来了麻烦。

我希望它们显示为所选项目,我可以从数据库中检索一些值。我可以从委托访问数据库,但是为了发送我的请求,我需要comboBox的行。

所以我想我的问题是:如何迭代表中的所有行并从自定义委托中执行某些操作?

如果它可以帮助我的自定义委托类:

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)

我从QTableView这样称呼它:

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

希望我很清楚,谢谢你的关注

1 个答案:

答案 0 :(得分:1)

不确定从代理访问数据库是否正确。您的委托可以包含对QTableView引用的QAbstractTableModel实例的引用。然后,您可以使用模型中的方法迭代表的行。

相关问题