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:
    Dialog.h

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

    Dialog.cpp

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

  • 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(); 
        ui->listView->setRootIndex(filemodel->setRootPath(sPath));
    

    I don't actually see where http://doc.qt.io/qt-5/qfilesystemmodel.html#setRootPath states what the QModelIndex it returns is. Did you take this code from somewhere?**

    ** EDIT: Hmm, I see it at http://www.bogotobogo.com/Qt/Qt5_QTreeView_QFileSystemModel_ModelView_MVC.php. 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">
     <class>Dialog</class>
     <widget class="QDialog" name="Dialog">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>597</width>
        <height>303</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Dialog</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QTreeView" name="treeView"/>
       </item>
       <item>
        <widget class="QListView" name="listView"/>
       </item>
      </layout>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

  • 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.

    @JNBarchan
    Ok, I will try this, thank you.


Log in to reply
 

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