QFileSystemModel is not updating correctly when setting filter for hidden items
-
Hi
I am using Qt 5.12.3 MS VS2017 on Windows Server 2016 x64. I am using a QFileSystemModel to show the file system and I have added an option to hide or show hidden items (files or folders). I have noticed that when the file permissions are set in File Explorer my application does not update correctly QFileSystemModel (it seems that the model does not detect the permissions change done from outside the application). Only by restarting the application I get the correct file system model.
This bug report refers to a similar issue:
https://bugreports.qt.io/browse/QTBUG-12413?jql=text ~ "QFileSystemModel"
Could someone shed more light into this problem ?
thanks
Bogdan -
Can you please create a minimal, compilable example so we can reproduce this, thx.
-
In the sample below (tested on Windows Server 2016 x64 with VS 2017 x64 and Qt 5.13.2) a root folder is set in order to install a file system watcher on it. The root folder contains another folder in it (initially this folder is not hidden). Start the application: should show the number of items in the root folder. From Windows Explorer change the folder attributes to hidden. This change is not detected by the file system model, but if you restart the application you will notice that the number of items in the root folder is decreased by one (i.e the hidden folder is detected).
Instead of restarting the application you might try to change the name of the hidden folder (the layout is not changed) or the name of another item (layout is changed and the number of items is shown correctly).
I would expect that this change in folder attributes to be detected, especially the hidden attribute change, since this has an impact on how the items are shown in a Tree view for example. Maybe someone could propose a solution for this problem.
#include <QGuiApplication> #include <QFileSystemModel> #include <QDebug> int main(int argc, char *argv[]) { QGuiApplication a(argc, argv); QFileSystemModel model; const auto filters = (QDir::AllEntries | QDir::NoDotAndDotDot) & ~QDir::Hidden; model.setFilter(static_cast<QDir::Filters>(filters)); const QString path = "E:/projects/temp"; model.setRootPath(path); QObject::connect(&model, &QFileSystemModel::layoutChanged, [&]() { qInfo() << "layoutChanged" << model.rowCount(model.index(path)); }); return a.exec(); }
-
Now I understand - so it's not a limitation of QFileSystemModel but QFileSystemWatcher. Looking at the code you can see that QFileSystemWatcher does not watch for attribute changes of directories:
https://code.woboq.org/qt5/qtbase/src/corelib/io/qfilesystemwatcher_win.cpp.html#403 , for a list of possible flags see here: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstchangenotificationaI would say it's an oversight and should be fixed especially since the inotify backend seems to watch for attribute changes of directories: https://code.woboq.org/qt5/qtbase/src/corelib/io/qfilesystemwatcher_inotify.cpp.html#285
Could not find a bug report about this so feel free to create one and link to this threat.
-
@Christian-Ehrlicher thank you for your quick reply
-
@cristeab said in QFileSystemModel is not updating correctly when setting filter for hidden items:
but how can I force an update on QFileSystemModel ?
You can't - you currently need to recreate the model
-
@Christian-Ehrlicher
If you didn't want to recreate, could you usesetRootPath()
(maybe twice, somewhere else then back to where it was) to get it to restart monitoring the directory, so that it would refresh and see the changed attribute? -
@JonB It's checking if it's the same path, so yes no need to set it to something else and revert it back. Or better - write a bugreport so it gets fixed in QFileSystemWatcher :)