从QTableView的自定义委托中选择comboBox的项目 [英] selected item of comboBox in custom Delegate from QTableView

查看:1003
本文介绍了从QTableView的自定义委托中选择comboBox的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



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

  class ComboBoxDelegate(QtGui.QItemDelegate):

def __init __(self,parent,itemslist):
QtGui .QItemDelegate .__ init __(自己,父母)
self.itemslist =物品清单
self.parent =父母

def paint(自我,画家,选项,索引):
#获取项目数据
值= index.data(QtCore.Qt.DisplayRole).toInt()[0]
#值= self.itemslist [index.data(QtCore.Qt.DisplayRole)。 toInt()[0]]
#使用项目数据填充样式选项
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionComboBox()
opt.currentText = str(self.itemslist [value])
opt.r ect = option.rect


#将项目数据绘制为ComboBox
style.drawComplexControl(QtGui.QStyle.CC_ComboBox,opt,painter)
self.parent.openPersistentEditor (索引)

def createEditor(自身,父项,选项,索引):

##获取行的检查值
# range(self.parent.model.rowCount(self.parent)):
#打印行

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)

返回self.editor

#def setEditorData(self,editor,index):
#值= 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)
如果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) ):
检查= 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))

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


解决方案

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


I use a custom delegate to display a column of comboBoxes in my QTableView. The values are the same for all the comboBoxes so it's not really the population part that gives me trouble.

I want them to show as the selected item, some value that I can retrieve from a database. I have access to the database from the delegate, but in order to send my request, I need the row of the comboBox.

So I guess my question is : how can you iterate over all the rows of the table and do some action from inside the custom delegate ?

If it can help here is my custom delegate class :

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)

And I call it fromthe QTableView like this :

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

Hope I was clear enough, thanks for your attention

解决方案

Not sure if accessing the database from the delegate is a right thing to do. Your delegate can contain reference to the instance of QAbstractTableModel which the QTableView refers to. You can then use methods in the model to iterate over rows of the table.

这篇关于从QTableView的自定义委托中选择comboBox的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆