1

有没有办法在给定行和列值的情况下编辑 qtableview 中的特定项目?例如,我想每秒增加它的值。这是我的表格模型。谢谢

class MyTableModel(QAbstractTableModel):

    def __init__(self, datain, headerdata, parent=None, *args): 
        """ datain: a list of lists
            headerdata: a list of strings
        """
        QAbstractTableModel.__init__(self, parent, *args) 
        self.arraydata = datain
        self.headerdata = headerdata


    def rowCount(self, parent):
        return len(self.arraydata) 

    def columnCount(self, parent):
        try:
            return len(self.arraydata[0]) 
        except:
            return 0
    def data(self, index, role): 
        if not index.isValid():
            return QVariant() 
        elif role != Qt.DisplayRole:
            return QVariant() 
        return QVariant(self.arraydata[index.row()][index.column()]) 

    def headerData(self, col, orientation, role):

        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
                return QVariant(self.headerdata[col])
        return QVariant()

    def sort(self, Ncol, order):
        """Sort table by given column number.
        """
        self.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))        
        if order == Qt.DescendingOrder:
            self.arraydata.reverse()
        self.emit(SIGNAL("layoutChanged()"))
4

1 回答 1

2

您可以直接在模型中增加值,并从模型中发出dataChanged信号。

例如,在模型类中添加这样的方法:

def incrementData(row, column):
    self.arraydata[row][column] += 1
    idx = self.index(row, column)
    self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"), idx, idx)
于 2012-04-23T10:05:43.650 回答