SetRootPath with QT, QFileSystemModel on Mac OS X

I am trying to create a file explorer in qt that works, but I cannot set the root path. I run Mac OS X and no matter what root path I embed, treeView always starts with

"/" top folder.

I spent 2 hours trying to figure it out.

if(QDir("SavedOutlines").exists()){ fileModel = new QFileSystemModel; QDir dir; QString rootpath = dir.absolutePath() + "/SavedOutlines/"; //QString path = "/Users/"; fileModel = new QFileSystemModel(this); QModelIndex idx = fileModel->setRootPath(rootpath); ui->treeView->setCurrentIndex(idx); ui->treeView->setModel(fileModel); ui->treeView->show(); } 

It seems that he does this first, and then returns to the top of the page again //

+1
source share
1 answer

QFileSystemModel always contains the entire file system, regardless of which rootPath selected.

You can limit what is shown in the view itself with QAbstractItemView::setRootIndex :

 QFileSystemModel *fileModel = new QFileSystemModel(this); ui->treeView->setModel(fileModel); ui->treeView->setRootIndex(fileModel->setRootPath(rootpath)); 
+1
source

All Articles