Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Show root path in QTreeview for QFileSystemModel.
I have a folder structure like this.
Now I want to show the contents of the above given directory in the same way as its shown above. Not only the contents but also the parent/root directory name.
QString path = "D:/Qt_Apps" QFileSystemModel* fileModel = new QFileSystemModel(this); fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files); //set root path fileModel->setRootPath(path); //create view QTreeView* view = new QTreeView(this); view->setModel(model);
If I run the above example code then I could see that only the contents of the given directory is displayed as given below
Here the parent directory is omitted.
Is there any way to show also the parent directory in the treeview structure??
I wonder if you use
with QFileSystemModels methods to get a QModelIndex,
If you can change what is Root.
Never tried it so might be stupid suggestion :)
Mammamia last edited by Mammamia
I have tried that also. But didn't help.
QString path = "D:/Qt_Apps"; QModelIndex indx = model->index(path); qDebug() << indx.data(); // QVariant(QString, "Qt_Apps") ... view->setRootIndex(indx); ...
Ok, was just a thought.
Im not aware of other method to show the root
when its not a drive.
I have repeated my test to verify how setRootIndex() works.
Suppose if we are currently in directory "D:/Qt_Apps"
Below code will show the contents of directory "D:/Qt_Apps/ColorApp"
QModelIndex indx = model->index("D:/Qt_Apps/ColorApp"); view->setRootIndex(indx);
Now we are in the directory "D:/Qt_Apps/ColorApp"
Below code will take us back to its parent directory "D:/Qt_Apps"
QModelIndex indx = model->index("D:/Qt_Apps"); view->setRootIndex(indx);
VRonin last edited by
It's not super starightforward. Reimplementing the model to is an option (and probably the most efficient one) . if you want something out of the box you can use KSelectionProxyModel
QString path = "D:/Qt_Apps" QFileSystemModel* fileModel = new QFileSystemModel(this); fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files); //set root path fileModel->setRootPath("D:/"); QItemSelectionModel* fileSelection=new QItemSelectionModel(fileModel,this); KSelectionProxyModel* subFolderProxy=new KSelectionProxyModel(fileSelection,this); subFolderProxy->setFilterBehavior(KSelectionProxyModel::SubTrees); fileSelection->select(fileModel->index("D:/Qt_Apps"),QItemSelectionModel::ClearAndSelect); //this is how you select subfolders //create view QTreeView* view = new QTreeView(this); view->setModel(subFolderProxy);
@VRonin thanks for the Suggestion.. I am planning to re implement the model by myself.
I have written my own model by subclassing QStandardItemModel.
I have written a blog post with the code and explanation which can be found here,
Custom FileSystemModel display
Thank you for sharing!