Unsolved QFileSystemModel update problem for shared network location
-
I'm using QFileSystemModel and QTreeView to display files tree from a shared network location.
void MainWindow::ListDirectory(QString arg_smb_path) { o_directory_system_model = new QFileSystemModel(this); o_directory_system_model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden); o_directory_system_model->setRootPath(arg_smb_path); ui->treeView->setModel(o_directory_system_model); ui->treeView->hideColumn(1); o_file_system_model = new QFileSystemModel(this); o_file_system_model->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden); o_file_system_model->setRootPath(arg_smb_path); ui->listView->setModel(o_file_system_model); ui->listView->setContextMenuPolicy(Qt::CustomContextMenu); }
The code works fine and I got the file tree. However, it can not automatically pick up the changes there. For example, if I create a new file there, the tree don't display this new file. When I set the path to a directory on local hard drive, like "D:/temp". It works. It picks up the changes and update itself. Any idea how can I pick up changes from a shared network location and refresh the model/tree?
-
@Azadshahr
This is a common problem for network locations for many operating systems/file systems. Not just Qt, e.g. you can often see this (lack of update) behaviour in, say, a File Explorer on a network path.If you think about it, you can see the host program may have trouble knowing that a remote network location has changed, it can't afford to poll all the time.
I don't know whether
QFileSystemModel
already uses this internally, but you should try aQFileSystemWatcher
on the network directory. If that does not notify of changes, it's not going to work. In that case, I would suggest you can only put in aQTimer
and periodically poll the remote to spot any changes, which is not great. You may well find that whether it works or not depends on the actual file system/network share protocol. Otherwise, I don't know if there is a Windows-only call you could use which can see the network update if the Qt code does not.