Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
         sPath = "/";  //OLD CODE : QString sPath = "/"; <----------------
        dirmodel = new QFileSystemModel(this);
        dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
        dirmodel->setRootPath(sPath);
        ui->dirView->setModel(dirmodel);
    
        filemodel = new QFileSystemModel(this);
        filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
        filemodel->setRootPath(sPath);
        ui->fileView->setModel(filemodel);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_dirView_clicked(const QModelIndex &index)
    {
        sPath = dirmodel->fileInfo(index).absoluteFilePath(); //OLD CODE :  QString sPath = dirmodel->fileInfo(index).absoluteFilePath(); <----------------
        ui->fileView->setRootIndex(filemodel->setRootPath(sPath));
    }
    

    My mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QFileSystemModel>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_dirView_clicked(const QModelIndex &index);
    
    private:
        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!
    Viktor

    My system: Linux debian jessie, Qt 5.9



  • 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



  • @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?

    regards



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

    Hi,

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


Log in to reply