我正在尝试更新我的QTableView,因为我通过pydispatcher收到系统更改的通知。我确实创建了以下函数
def rowCount(self, parent=None):
return len(self.m_list)
def columnCount(self, parent=None):
return len(self.table_def)
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.table_def[col]['Header']
return QVariant()
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
if index.row() >= len(self.m_list) or index.row() < 0:
return None
row = index.row()
col = index.column()
print("Called for (%s, %s, %s)" % (row, col, role))
if role == Qt.DisplayRole:
return self.table_def[index.column()]['Function'](index.row())
elif role == Qt.BackgroundRole:
batch = (index.row() // 100) % 2
if batch == 0:
return QApplication.palette().base()
return QApplication.palette().alternateBase()
else:
return None
def flags(self, index):
if not index.isValid():
return None
return Qt.ItemIsEnabled
def update_model(self, data):
print('update_model')
index_1 = self.index(0, 0)
index_2 = self.index(0, 1)
self.dataChanged.emit(index_1, index_2, [Qt.DisplayRole])
self.dataChanged.emit(index_1, index_2, [Qt.DisplayRole])
行似乎没有任何作用;即 不调用data(self, index, role=Qt.DisplayRole)
。
如果我单击该表,则会调用data(self, index, role=Qt.DisplayRole)
并更新该表。
我现在的修复程序是调用beginResetModel((和endResetModel((。这行得通,但这不是它应该如何工作。
知道会发生什么吗?
我遇到了同样的问题,我只是通过调用self.headerDataChanged.emit来修复它。因此,为此,一旦您更改了表中的某些内容,请调用以下内容:
self.headerDataChanged.emit(Qt.Horizontal, idx1, idx2)
其中self._data是类中的数据。idx1 和 idx2 分别是已更改数据的第一个和最后一个索引。 Qt.Horizontal
是一个示例,根据您的表格内容,它可能是垂直的。