QFileSystemModel::setRootPath behavior does not match documentation under Windows
-
The Qt documentation says about QFileSystemModel::setRootPath():
Sets the directory that is being watched by the model to newPath by installing a file system watcher on it. Any changes to files and directories within this directory will be reflected in the model.
https://doc.qt.io/qt-6/qfilesystemmodel.html#setRootPath
Lets say I use setRootPath in the following way:
auto model = new QFileSystemModel; model->setRootPath("C:\\folder"); treeView->setModel(model);
According to the documentation, the model should now track changes in the specified path C:\folder. However, the model also tracks changes outside of C:\folder. Changes are tracked even on other disks.
Why is this happening? How to correctly understand the work of this method? If QFileSystemModel monitors all paths that are visible in QTreeView, then why does the model require a specific path to be monitored? After all, all paths are monitored anyway.
-
@Jo-Jo
You can find out about this by Google (e.g.qfilesystemmodel setrootpath
).I think you will find that e.g. https://stackoverflow.com/a/49617341/489865 deals with this:
You must indicate to the QTreeView what is your root item with setRootIndex():
model.setRootPath(QDir.rootPath()) self.setRootIndex(model.index("C:"))
So you have to the
QTreeView::setRootIndex()
as well as theQFileSystemModel::setRootPath()
? -
@JonB Thank you, but I'm not asking about what the view shows. The question is, if all paths that are visible in QTreeView are monitored by the model anyway, why do you need to use setRootPath to set the monitoring path? After all, all paths are monitored anyway after setRootPath
-
@Jo-Jo
Yes, I understand.First, just in case, verify that your
model->setRootPath("C:\\folder")
returns a validQModelIndex
and try withC:/folder
instead just in case. To get that out of the way.Then see QFileSystemModel::setRootPath behavior does not seems to match the documentation to see that you are not alone in this question and, from what I can see, nobody knows why/the exact behaviour! They just care about getting their
QTreeView
showing the right stuff :) It might be that your behaviour is Windows-only, like I said before monitoring code depends on platform offerings and what underlying call or implementation that and Qt use. You could try to see how the root path is actually used in the Windows code. -
How do you know that other paths are also tracked?
The internal object for sure only looks in the root path specified. -
@Christian-Ehrlicher
That sounds more like it! But remember the stackoverflow post also claims:The whole file system is being watched. Even if I make a change in E drive I can notice the same getting reflected in the view (model). This behavior seems to be different from the documentation.
-
How do you see a change when the view does not show this path?
-
@Christian-Ehrlicher said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:
How do you see a change when the view does not show this path?
I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:
If you set C: as setRootPath, then QTreeView will show changes in both C: and D:
-
@Jo-Jo said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:
I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:
If you set C: as setRootPath, then QTreeView will show changes in both C: and D:
I don't understand - when you set C:\ as the root path why/how to you see the contents of D:\ ?
-
@Christian-Ehrlicher said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:
I don't understand - when you set C:\ as the root path why/how to you see the contents of D:\ ?
I think you are confusing QFileSystemModel::setRootPath and QTreeView::setRootIndex