how to update ( setvalue , sorting , editing , background color ) QProgressBar (QitemDelegate) inside QTableview (QAbstractTableModel)

StackOverflow https://stackoverflow.com/questions/21877980

Question

I am a NEWBIE dev on python , this is my first python script . I added a QProgressBar to QTableview (QAbstractTableModel) as a QItemDelegate and it works. QProgressBar did show up , but it cant update automatically when i edit or sort the QTableview. QItemDelegate 's Background color , alignment , edit are not follow the role? any idea ? Do i need to use setmodeldata or seteditordata ? any example in this case?

here's the class of 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)

here's the delegate code :

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
            )
Was it helpful?

Solution

data updated can been fixed by using updateEditorGeometry but not background color :

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top