0

As per the Qt5.11 documentation, 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.

But in the case of my test code, a part of which is as below:

auto model = new QFileSystemModel(this);
model->setRootPath("C:/adb");
ui.treeView->setModel(model);

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. What am I missing here?

Sajal
  • 1,783
  • 1
  • 17
  • 21

1 Answers1

0

I assume your question is similar to this, but the link in that answer is broken!

So, That's true for the model, same documentation says:

Note: This function does not change the structure of the model or modify the data available to views. In other words, the "root" of the model is not changed to include only files and directories within the directory specified by newPath in the file system.

So in order to set your view to show only that path, you need to set your view to the needed index of the model:

QModelIndex idx = model->index(model->rootPath());
ui->treeView->setRootIndex(idx);
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • The question is not about the content of the `QTreeView`, it's about the `QFileSystemWatcher` that gets installed upon calling the `setRootPath` API. – Sajal Aug 12 '18 at 15:26
  • I know, your question was "_Even if I make a change in E drive I can notice the same getting reflected **in the view**_" , the documentation note above says "This function does not modify the model or the data available to views" which means you will notice the change in the view – Mohammad Kanan Aug 12 '18 at 15:41