QFileSystemModel setRootPath

I am trying to create a Qt application that displays the contents of a folder (Users folder on Mac OS). Here is the code:

QFileSystemModel *dirModel = new QFileSystemModel; dirModel->setRootPath("/Users"); ui->listView->setModel(dirModel); 

I also tried using this code

When I launch the application, instead of showing the contents of the "/ Users" folder, it shows the root drive (note: not the contents of the drive). A folder exists, and I also tried using other folders.

+7
source share
2 answers

Have you tried to get the index to show the directory?

  listView->setRootIndex(dirModel->index("/Users")); 

This works fine for me:

 #include <QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QFileSystemModel model; model.setRootPath("/Users"); QListView view; view.setModel(&model); view.setRootIndex(model.index("/Users/")); view.show(); return app.exec(); } 
+9
source

This code worked for me too:

 QFileSystemModel *dirModel = new QFileSystemModel(this); dirModel->setRootPath("/Users"); ui->listView->setModel(dirModel); ui->listView->setRootIndex(dirModel->setRootPath("/Users")); 
+5
source

All Articles