ecbb
ecbb

Reputation: 141

Qt QFileSystemModel - Multiple directory trees under the root index

I'm trying to create a QTreeView using QFileSystemModel that will display multiple directory trees under the root index of the tree.

I'm currnetly using the following code:

QFileSystemModel *model = new QFileSystemModel();
QTreeView *tree = new QTreeView();
model->setRootPath(QDir::rootPath());
tree->setModel(model);

Using this, the file tree will display a single item (the root of the file system). What I'd like to do, though, is be able to display the root of the file system and have several specific directories and unrelated subtrees as siblings.

Below is an image of the desired behaviour from a different application. The first two items are specific folders within the file system, acting like shortcuts. The last item is unrelated to the file system.

desired behaviour

Upvotes: 9

Views: 2857

Answers (2)

Caleb Koch
Caleb Koch

Reputation: 874

You should be able to create a temporary directory (using QTemporaryDir) and then create symbolic links within that temporary directory that point to the files and/or directories you want to show. Then use the temporary directory's path as the model's root path.

Upvotes: 2

user360907
user360907

Reputation:

I would suggest creating a custom model, perhaps derived from QAbstractItemModel, that had some top level entity (off screen) to which you could parent multiple QFileSystemModels, one for each top level directory you wanted to display. This would allow you to manipulate each tree in a different way, depending on what type of directory it was displaying.

Upvotes: 5

Related Questions