Python 如何在QTableview(QabStretctTableModel)中更新(设置值、排序、编辑、背景色)QProgressBar(QitemDelegate)

Python 如何在QTableview(QabStretctTableModel)中更新(设置值、排序、编辑、背景色)QProgressBar(QitemDelegate),python,qtableview,qabstracttablemodel,qprogressbar,Python,Qtableview,Qabstracttablemodel,Qprogressbar,我是python的新手开发人员,这是我的第一个python脚本。 我将QProgressBar作为QItemDelegate添加到QTableview(QAbstractTableModel)中,它可以正常工作。 QProgressBar确实出现了,但当我编辑或排序QTableview时,它无法自动更新。 QItemDelegate的背景色、对齐方式、编辑是否不符合角色要求? 有什么想法吗?我是否需要使用setmodeldata或seteditordata?这方面有什么例子吗 这是QAbstra

我是python的新手开发人员,这是我的第一个python脚本。 我将QProgressBar作为QItemDelegate添加到QTableview(QAbstractTableModel)中,它可以正常工作。 QProgressBar确实出现了,但当我编辑或排序QTableview时,它无法自动更新。 QItemDelegate的背景色、对齐方式、编辑是否不符合角色要求? 有什么想法吗?我是否需要使用setmodeldata或seteditordata?这方面有什么例子吗

这是QAbstractTableModel的类

def data(self, index, role):
    col = index.column ()
    row = index.row()
    if not index.isValid():
        return QVariant()
    elif role == Qt.BackgroundColorRole and row%2 == 0 :
        return QVariant(QColor(60,60,60))            
    elif role == Qt.DisplayRole and col != 3 :
        return QVariant(self.arraydata[row][col])
    elif role == Qt.EditRole and col != 3 :
        return QVariant(self.arraydata[row][col])
    elif role == Qt.ToolTipRole :
        return QVariant("tool tips")
    elif role == Qt.TextAlignmentRole:
        return QVariant(Qt.AlignCenter | Qt.AlignCenter)
以下是代理代码:

class progressDelegate(QItemDelegate):
    def __init__(self, parent  ):

        QItemDelegate.__init__(self, parent )
        self.tb = self.parent().shotslistTable 
        self.tm = self.parent().tm

    def paint(self, painter, option, index):

        self.pbar = QProgressBar(self.parent())
        col = index.column ()
        row = index.row ()
        self.pbar.setMinimum(0)
        self.pbar.setMaximum(100)
        self.pbar.setValue (int(self.tm.arraydata[row][col]))
        self.pbar.setMaximumHeight(24)
        if not self.tb.indexWidget(index):
            self.tb.setIndexWidget(
                index, 
                self.pbar
            )

更新的数据可以通过使用updateEditorGeometry而不是背景色进行修复:

def updateEditorGeometry( self, editor, option, index ):
    # input data by here will update automatically 
    col = index.column ()
    row = index.row ()
    val = int(self.tm.arraydata[row][col])
    editor.setValue (val)

    editor.setGeometry(rect)