如何在QTableview(QAbstractTableModel)中更新(setvalue,排序,编辑,背景颜色)QProgressBar(QitemDelegate)

时间:2014-02-19 10:39:10

标签: python qtableview qabstracttablemodel qprogressbar

我是python上的NEWBIE开发者,这是我的第一个python脚本。 我将QProgressBar添加到QTableview(QAbstractTableModel)作为QItemDelegate并且它可以工作。 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
            )

1 个答案:

答案 0 :(得分:0)

更新的数据可以使用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)