我使用Python和pyside2qt绑定。在
我的程序旨在从csv文件加载记录,将它们显示为表中的行,并在被要求时,将每个记录上载到远程数据库。每次上传都需要几秒钟的时间,所以我想在上传的时候改变每一行的背景颜色,然后根据成功与否再次将其更改为红色或绿色。在
我有一个扩展TableModel
的类。该程序不需要编辑这些值,只需从csv加载它们,所以它不实现setData()
,而只实现data()
。为了排序,我让它通过一个扩展的QSortFilterProxyModel
进入QTableView
。在
class TableModel(QAbstractTableModel):
records = [] #Where the list of records is kept
def data(self, index, role=Qt.DisplayRole):
record = self.records[index.row()]
if role == Qt.DisplayRole:
#bunch of table data stuff
elif role == Qt.BackgroundColorRole:
#This gets called all the time
#but is never called during the uploading process
if record.uploading: return QColor.cyan
def upload(self):
for recordRow in range(len(self.records)):
record = self.records[recordRow]
start = self.createIndex(recordRow, 0)
end = self.createIndex(recordRow, 4)
record.uploading = True
#I've tried both explicitly specifying the desired role
#as well as omitting the argument
self.dataChanged.emit(start, end, [Qt.BackgroundColorRole])
record.upload() #Currently just waits for 1 second
record.uploading = False
self.dataChanged.emit(start, end, [Qt.BackgroundColorRole])
如您所见,我设置了一个上载标志,发出dataChanged
信号,上载(实际上现在只等待1秒),关闭该标志,然后再次发出dataChanged
。我希望看到青色高亮显示在每一行停留一秒钟,沿着列表向下移动,但是没有发生任何事情。在
当我监视data()
方法时,在上载迭代期间,它永远不会被BackgroundColorRole
调用。在
我还将一个测试方法连接到dataChanged
信号,它确实以正确的索引发出。在
是否需要执行其他操作才能正确连接dataChanged
?我的模型和视图之间的QSortFilterProxyModel
是否会导致问题?在
在主线程中,您不应该有延迟超过30毫秒的任务,因为它会阻止GUI,避免执行事件循环,因此信号不会通知,从而导致GUI的更新不会发生。所以您应该在线程上运行它,或者最好使用QtNetwork,因为它与Qt事件循环很友好。在
相关问题 更多 >
编程相关推荐