# QFileSystemModel: include files from subfolders (Qt 5.5.1)

• Hello!

Atm i use QFileSystemModel to get a filtered list of files and view them in a listview widget. But so far i fail on including files from subfolders as well. I did study the documentation up and down and google my way through a few 7 year old postings, but so far i did not find any example code how it may be possible to do this.

For now i experiment with QDirIterator instead of QFileSystemModel but it is kinda slow and locks the app (depending on how much there is) for a few seconds. But i'm able to get all the files, at least in a nice qDebug output.

Is there any working example out there on how to use QFileSystemModel and literate form a given root path through all subfolders? I'm using Qt 5.5.1.

Thank you!

• Hi,

QFileSystem is optimized to avoid consuming unneeded resources i.e. don't load everything at once so your use case looks like a candidate for a QDirIterator working in another thread and using signals and slots to send data upper in the chain.

• @SGaist
Thank you, but at this point i don't even know how to add files to the model. Later on i want to give the user a choice to also search and include files in sub directories.

files= new QFileSystemModel(this);
files->setNameFilters(myFilter);
files->setNameFilterDisables(false);
files->setRootPath(myPath);
ui->listView1->setModel(files);
ui->listView1->setRootIndex(files->setRootPath(myPath));


If there are files i filter for, i get them listed in listView1. So far so good, but how to include also files from sub directories?

QDirIterator subs(myPath, myFilter, QDir::Files, QDirIterator::Subdirectories);
while(subs.hasNext())
{
qDebug() << subs.next();
}


Thats the way i do it now but how can i add them to QFileSystemModel? There is no ui->listView1->append(subs.next()); or is there? I guess some basic stuff but i am unable to find a solution by myself so far.

Thanks!

• You don't add paths to QFileSystemModel. That class represent your filesystem model starting at the root of the drive.

QFileSystemModel will load additional data if you browse to folders you are interested in.

• So the only solution is to change models? If i want to include files from sub directories i have to change the model from QFileSystemModel to QStringListModel for example and use QDirIterator?

• What do you want to use it for ?

• The goal here is to find text files in a given path and list them in a QListView. If the user clicks on a text file in this list, he should be able to rename it and on a double click to edit the text file. So far so good, i got this covered.

In some rare cases text files are sorted inside sub directories, one by one - means 20 text files are in 20 sub directories. It is kinda lame to click through all the subs so my idea was to add a button or check box to also include all text files in this given root path.

The first part was easy and fast for me to accomplish but the second part - including text files from sub directories and add them to the QListView / QFileSystemModel is still a mystery to me. I'm still a Qt beginner, any help is appreciated.

Thank you!

• Then a model based on QAbstractListModel that you populate using QDirIterator might be the simple way to go.

• This is not an option, i rather give up on this and show additional files in a log window or any other way. If the user browses through directories and clicks C: for example, the app will lock for seconds or minutes if no ssd is present.

Is it really that complicated to include sub directories in QFileSystemModel?

• Just to be sure I understand you correctly, your additional folders should act like a QFileSystemModel with a QFileSystemModel ?

• I just need them all together in the listview widget i got for files from the QFileSystemModel and i need the paths to the files so i can open them for edit.

• I sort of found a solution that works. On button click i change the listview model from QFileSystemModel to QStringListModel and call a function and use QDirIterator to add filenames to a QStringList. It needs additional code but if QFileSystemModel can't be set to a recursive directory mode, i don't have a lot of options, i guess.

Is there any better solution?

Thanks!

• Like I wrote, QFileSystemModel is optimized to avoid filing up your memory with everything it can find on the hard drive.

Your use case really sounds like QDirIterator is a better choice to retrieve the information you want.

• I don't need all the files from a hard drive, just the files in all the sub directories inside directory. For example on Windows: C:\text-files\2016*.txt and all the *.txt files from C:\text-files\2016\1*.txt C:\text-files\2016\2*.txt and so on. I don't need files from C:\Windows or C:\Programs or ...

Edit: Editor is removing \ before * so please imagine them there! ^^

• That I understood. That's why I think that QDirIterator is a better tool to find what you want on your drive.

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.