Important: Please read the Qt Code of Conduct -

QFileSystemModel with filter QDir::Files shows not only files, but also opened subfolders

  • Hello. I'm new with Qt and I'm following video tutorial about QFileSystemModel. So, I'm making file explorer with two views - one is displaying only folders (with treeView), the other one shows only files from the folder, selected in first view.
    Everything works fine, but if I choose folder with subfolder, click on subfolder, and then click on parent folder - it will show previously selected subfolder in the list, that is supposed to show files only. Not sure if it's bug or I did something wrong.
    BTW, I am on Mac, but somebody already asked about same problem in comments (without answer), not sure about his OS.
    Here is the code:

    class Dialog : public QDialog
        explicit Dialog(QWidget *parent = 0);
    private slots:
        void on_treeView_clicked(const QModelIndex &index);
        Ui::Dialog *ui;
        QFileSystemModel *dirmodel;
        QFileSystemModel *filemodel;


    Dialog::Dialog(QWidget *parent) :
        ui(new Ui::Dialog)
        QString sPath = "/users/Max/";
        dirmodel = new QFileSystemModel(this);
        dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
        QModelIndex index = dirmodel->index(sPath, 0); 
        filemodel = new QFileSystemModel(this);
        filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
        index = filemodel->index(sPath, 0); 
        delete ui;
    void Dialog::on_treeView_clicked(const QModelIndex &index)
        QString sPath = dirmodel->fileInfo(index).absoluteFilePath(); 

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ?
    On what version of macOS ?

  • Hello. I'm using Mac OS 10.13.1 and version of Qt is 5.9.2
    I also just tested it on Windows 10 x64 (with Qt 5.9.2) and it has the same issue.

    alt text

  • @Maxxii

        QString sPath = dirmodel->fileInfo(index).absoluteFilePath(); 

    I don't actually see where states what the QModelIndex it returns is. Did you take this code from somewhere?**

    ** EDIT: Hmm, I see it at And someone said on your youtube that it has same problem. Perhaps an expert can say there is a problem in that code?

  • Lifetime Qt Champion

    @Maxxii Can you also provide the .ui file for your example ?

  • Sure. But there is nothing more than 1 treeView and 1 listView.

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <widget class="QDialog" name="Dialog">
      <property name="geometry">
      <property name="windowTitle">
      <layout class="QHBoxLayout" name="horizontalLayout">
        <widget class="QTreeView" name="treeView"/>
        <widget class="QListView" name="listView"/>
     <layoutdefault spacing="6" margin="11"/>

  • Lifetime Qt Champion

    Even if simple, it avoids people trying to help you to have to rewrite it and guess what's in it and thus lose time to re-create a functional application.

    So there might be bug here with QFileSystemModel. I'd recommend taking a look at the bug report system to see if it's something known. If not please consider opening a new report providing the complete code sample.

  • @Maxxii
    If this does indeed turn out to be a bug, as @SGaist suggests, and is something to do with changing the root path not quite respecting the filter and showing sub-driectories under some circumstances, to work around for now try something like refreshing or even recreating the filemodel in your on_treeView_clicked(), till it's fixed.

  • @SGaist
    Ok. I thought I have to paste minimum amount code on forum, so thank you for clarification.
    I wanted to report this as bug, but decided to post it here first, just to be sure it's not about my misunderstanding.

    Ok, I will try this, thank you.

Log in to reply