Show root path in QTreeview for QFileSystemModel.
-
I have a folder structure like this.
- Qt_Apps
- Demo1
- Demo2
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
- Demo1
- Demo2
Here the parent directory is omitted.
Is there any way to show also the parent directory in the treeview structure?? - Qt_Apps
-
Hi
I wonder if you use
http://doc.qt.io/qt-5/qabstractitemview.html#setRootIndex
with QFileSystemModels methods to get a QModelIndex,
http://doc.qt.io/qt-5/qfilesystemmodel.html#index-1
If you can change what is Root.
Never tried it so might be stupid suggestion :) -
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"
Test-1
Below code will show the contents of directory "D:/Qt_Apps/ColorApp"QModelIndex indx = model->index("D:/Qt_Apps/ColorApp"); view->setRootIndex(indx);
Test-2
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);
-
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);
-
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