[英]Pyqt5 QAbstractTableModel dataChanged not updating data
在我通过 pydispatcher 收到有关系统更改的通知后,我正在尝试更新我的 QTableView。 我确实创建了以下功能
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
是一个示例,它可能是垂直的,具体取决于您的表格内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.