QDir::Files doesn't work as expected

  • Hi everybody,

    I'm playing around with a little program, which should be a simple kind of file manager. I've two widgets in my main window, a treeview and a listview. In the treeview only directories are shown, and in the listview all files are shown, wich are available in the clicked directory. This works basically, but:

    When I click in the treeview through different directory levels and go back to a higher level, I see always the clicked directories from one level below in my listview, which the filter QDir::Files should prevent...

    My mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
         sPath = "/";  //OLD CODE : QString sPath = "/"; <----------------
        dirmodel = new QFileSystemModel(this);
        dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
        filemodel = new QFileSystemModel(this);
        filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
        delete ui;
    void MainWindow::on_dirView_clicked(const QModelIndex &index)
        sPath = dirmodel->fileInfo(index).absoluteFilePath(); //OLD CODE :  QString sPath = dirmodel->fileInfo(index).absoluteFilePath(); <----------------

    My mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QFileSystemModel>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void on_dirView_clicked(const QModelIndex &index);
        Ui::MainWindow *ui;
        QFileSystemModel *dirmodel;
        QFileSystemModel *filemodel;
        QString sPath; //NEW CODE <----------------
    #endif // MAINWINDOW_H

    Have someone an idea, how to fix this behaviour of my code?

    Thanks in advance!

    My system: Linux debian jessie, Qt 5.9

  • Moderators

    Hi cheops , welcome to the Qt forums ,

    You have used the same variable name in 2 different scopes :
    QString sPath is in your constructor, but also in your slot.

    Make it a member variable of your mainwindow class. This makes both variables the same and you should be good to go.


  • @Eddy
    Hy Eddy,

    many thanks for your reply! But obviously I undertood something wrong... I declared the variable sPath in the mainwindow.h under private, now it should be a member variable, or am I wrong?

    But this doesn't change anything... :-( What did I understood wrong?


  • Moderators

    @cheops said in QDir::Files doesn't work as expected:

    I declared the variable sPath in the mainwindow.h under private, now it should be a member variable

    I don't see that in the code snippet in your first post.
    What I can do for you is mark the lines in your post that should change.

    EDIT : I marked the lines with //OLD CODE, //NEW CODE and <------------

  • Lifetime Qt Champion


    What @Eddy pointed you to is that you were shadowing your private variable in your constructor hence your unexpected results.

Log in to reply

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