Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a QString fileName which stores in it a path to a subdirectory (eg. C:/Qt). I also have a treeView inside a dialog box.
Upon starting the application, we browse a file and once the file/folder is selected, I save the path of the file/folder in fileName string.

Upon clicking "OK" (QPushButton), I intend to open a new Dialog Box divided into a treeView displaying the subdirectory with the selected folder as the root node of the tree, and a listView displaying all the files/folders within that folder and nothing if empty.

I used the following QFileSystemModel functions to display the C:/ directory (only drive on my pc) :

What I have tried:

QString path;

//QFileSystemModel *dirModel;
//QFileSystemModel *fileModel;
//TreeView
dirModel = new QFileSystemModel;
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(path);
ui->treeView->setModel(dirModel);

//ListView
fileModel = new QFileSystemModel;
fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
fileModel->setRootPath(path);
ui->listView->setModel(fileModel);


If I set the path to fileName (QString path = fileName;), nothing changes. I'm assuming QDir::AllDirs is the reason for this.

I looked for other QDir filters (like QDir::Dirs or setFilter) but nothing seems to work. I even tried QDesktopServices::openUrl, but it just opens the file path in a new window instead of the treeView.


If I set the path to fileName (QString path = fileName;), nothing changes. I'm assuming QDir::AllDirs is the reason for this.

I looked for other QDir filters (like QDir::Dirs or setFilter) but nothing seems to work. I even tried QDesktopServices::openUrl, but it just opens the file path in a new window instead of the treeView.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900