我遇到QFileSystemModel没有显示文件更改的问题。首次创建文件时,它会立即显示。但是当文件本身发生更改时,大小和时间戳不会更新。我已尝试多次尝试强制模型更新但没有取得真正的成功。我所取得的最好成就是完全取代模型。虽然这会导致此错误:
QSortFilterProxyModel: index from wrong model passed to mapToSource
下面的测试代码创建了一个空目录的表视图。单击左键会创建一个文件(foo.txt)。连续点击会将数据附加到文件中。我的理解是QFileSystemModel不需要刷新,但第二个按钮是我的尝试。
对于我做错的任何帮助都将不胜感激!
# Testing with python3.6.3 and pip installed pyqt5 5.9.2 in virtualenv on Ubuntu
import os, sys, tempfile
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
self._view = QtWidgets.QTableView()
layout.addWidget(self._view)
self._modify_button = QtWidgets.QPushButton('Create')
layout.addWidget(self._modify_button)
self._refresh_button = QtWidgets.QPushButton('Refresh')
layout.addWidget(self._refresh_button)
self._modify_button.clicked.connect(self._modify)
self._refresh_button.clicked.connect(self._refresh)
self._model, self._proxy = None, None
self.temp_dir = tempfile.TemporaryDirectory(dir=os.path.dirname(os.path.abspath(__file__)))
self.init_model(self.temp_dir.name)
def init_model(self, path):
self._model = QtWidgets.QFileSystemModel()
self._model.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.AllEntries)
self._proxy = QtCore.QSortFilterProxyModel(self)
self._proxy.setSourceModel(self._model)
self._view.setModel(self._proxy)
# self._view.setModel(self._model)
self._model.directoryLoaded.connect(self._loaded)
self._model.setRootPath(path)
def _loaded(self):
path = self._model.rootPath()
source_index = self._model.index(path)
index = self._proxy.mapFromSource(source_index)
self._view.setRootIndex(index)
# self._view.setRootIndex(source_index)
def _modify(self):
"""Create or modify foo.txt..model should see and update"""
self._modify_button.setText('Modify')
file_name = os.path.join(self.temp_dir.name, 'foo.txt')
with open(file_name, 'a') as txt_file:
print('foo', file=txt_file)
# def _refresh(self):
# # This only seems to work once..and its a flawed approach since it requires permission to write
# temp = tempfile.NamedTemporaryFile(dir=self.temp_dir.name)
# def _refresh(self):
# self._model.beginResetModel()
# self._model.endResetModel()
# def _refresh(self):
# self._proxy.setFilterRegExp('foo')
# self._proxy.setFilterRegExp(None)
# self._proxy.invalidate()
# self._proxy.invalidateFilter()
# self._proxy.reset()
#
# root_index = self._model.index(self._model.rootPath())
# rows = self._model.rowCount(root_index)
# proxy_root_index = self._proxy.mapFromSource(root_index)
# topLeft = self._proxy.index(0, 0, proxy_root_index)
# bottomRight = self._proxy.index(rows - 1, self._model.columnCount(proxy_root_index) - 1, proxy_root_index)
# # self._proxy.dataChanged.emit(topLeft, bottomRight)
# self._model.dataChanged.emit(topLeft, bottomRight)
# def _refresh(self):
# # This only seems to work once
# self._model.setRootPath('')
# self._model.setRootPath(self.temp_dir.name)
def _refresh(self):
# This seems heavy handed..but seems to work
# ..though generates "QSortFilterProxyModel: index from wrong model passed to mapToSource" spam in console
self.init_model(self.temp_dir.name)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
答案 0 :(得分:3)
答案 1 :(得分:0)
从Qt v5.9.4开始,您可以设置环境变量QT_FILESYSTEMMODEL_WATCH_FILES
,您可以在changelog中了解更多相关信息:
[QTBUG-46684]现在可以启用每个文件观看 设置环境变量QT_FILESYSTEMMODEL_WATCH_FILES, 允许跟踪文件大小的变化。
一些事情: