0

I have a drive labeled as "Local Data" mounted at "/media/Local Data". And my program directory is also in "/media/Local Data/Programming". I would like to set "Local Data" to be the root path of a QFileSystemModel. I cannot find a way to do it.

Here is my code:

QFileSystemModel* model = new QFileSystemModel;
model->setRootPath("/media/Local Data");

QTreeView tree;
tree.setRootIndex(model->index("/media/Local Data");
tree.setModel(model);
qDebug() << QDir::currentPath();
// "/media/Local Data/Files/Programming/C++/build-FileSystemModel-Desktop_Qt_5_5_1_GCC_64bit-Debug"

tree.setMinimumSize(900,500);
tree.show();

I get the error QAbstractItemView::setRootIndex failed : index must be from the currently set model.

When I run the program, it always give me the root "/" as the root path.

How can I achieve this?

tom
  • 1,302
  • 1
  • 16
  • 30

1 Answers1

0

That error is not related to the QFileSystemModel. You haven't set a model for the view. Try this (untested).

QFileSystemModel* model = new QFileSystemModel;
model->setRootPath("/media/Local Data");

QTreeView* tree = new QTreeView;
tree->setModel(model);
tree->setRootIndex(tree->model()->index(QDir::currentPath()));
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35