Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDir::Files doesn't work as expected
Forum Update on Tuesday, May 27th 2025

QDir::Files doesn't work as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cheopsC Offline
    cheopsC Offline
    cheops
    wrote on last edited by Eddy
    #1

    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

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by Eddy
      #2

      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

      Qt Certified Specialist
      www.edalsolutions.be

      cheopsC 1 Reply Last reply
      1
      • EddyE Eddy

        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

        cheopsC Offline
        cheopsC Offline
        cheops
        wrote on last edited by
        #3

        @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

        EddyE 1 Reply Last reply
        0
        • cheopsC cheops

          @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

          EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by Eddy
          #4

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

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved