I want to initialize the starting path in Qt treeview to the user home on Linux Ubuntu.
I tried this code but it does not work.
QString sPath = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx";
dirmodel = new QFileSystemModel(this);
dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirmodel->setRootPath(sPath);
ui->treeView->setModel(dirmodel);
/home/<username>
${HOME}
/home/casper/Music
No one works. =[
How to solve this?
Thank you for your help.
Once the model is loaded, I think you need to call setRootIndex
on the tree widget:
ui->treeView->setRootIndex(dirmodel->index("/home/casper"));
Or for any home directory (also works on Windows, OSX, etc.):
ui->treeView->setRootIndex(dirmodel->index(QDir::homePath()));