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
)
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)