There are 2 problems.
The first described by Frank Osterfeld . Moving:
QListWidgetItem *newItem = new QListWidgetItem;
into your cycle.
The second is related to the QFileSystemModel model. from the docs for QFileSystemModel :
Unlike QDirModel, QFileSystemModel uses a separate thread to populate, so it will not cause the main thread to hang when the file system is requested. Calls to rowCount () return 0 until the model populates the directory.
and
Note. QFileSystemModel requires an instance of a GUI application.
I do not think that QFileSystemModel() will work properly until the Qt loop cycle is started (which is launched by a.exec() in your example).
In your case, model->rowCount(parentIndex) returns 0, even if it has elements in it (at least what it does in my test).
Replacing QFileSystemModel with QDirModel (and removing the call to model->setRootPath(dir) , which is not supported by QDirModel) fills the list.
source share