0

我是 python 的新手开发者,这是我的第一个 python 脚本。我将 QProgressBar 作为 QItemDelegate 添加到 QTableview (QAbstractTableModel) 并且它可以工作。QProgressBar 确实出现了,但是当我编辑或排序 QTableview 时它不能自动更新。QItemDelegate 的 Background color , alignment , edit 不跟角色?任何想法 ?我需要使用 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
            )
4

1 回答 1

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)
于 2014-02-24T04:46:20.197 回答