Reputation: 533
I'm trying to create a file explorer in qt which is working but I cannot set the root path. I am running Mac OS X and no matter what root path I put in, the treeView always begins with
"/" the top folder.
I've spent 2 hours trying to figure this 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 like it's doing it at first and then resetting itself back to the top "/"
Upvotes: 1
Views: 1725
Reputation: 29896
QFileSystemModel
always contains the whole filesystem, regardless of the rootPath
that was chosen.
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));
Upvotes: 1