【问题标题】:selected item of comboBox in custom Delegate from QTableView来自 QTableView 的自定义委托中的组合框的选定项
【发布时间】:2011-08-22 07:15:06
【问题描述】:

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

我希望它们显示为选定的项目,即我可以从数据库中检索的一些值。我可以从代理访问数据库,但为了发送我的请求,我需要组合框的行。

所以我想我的问题是:如何遍历表的所有行并从自定义委托内部执行一些操作?

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

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))

希望我足够清楚,感谢您的关注

【问题讨论】:

    标签: combobox pyqt qtableview qitemdelegate


    【解决方案1】:

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

    【讨论】:

    • 好吧,我设法在组合框中显示了正确的选定项目。但正如你所说,我正在从代表访问数据库,但我真的不知道如何实现你的建议。而且我仍然有一个问题:当我重新排序 tableView 时,组合框不会“跟随”..
    • 是否可以从模型中调用委托的重绘?
    • 但是模型如何知道委托,好的,您可以将委托引用传递给模型。委托是他们改进演示(即更好的视图)的任务,使模型和委托相互依赖会使您的代码变得脆弱。
    • 现在我在模型中没有委托的实例。实际上,我有一个模型实例和 viex 上的一个代表,所以我有“相互访问”,但它们并不是真正相互依赖的。我试图从我的代码中几乎所有地方更新委托,似乎没有任何工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多