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. Using QFileSystemModel, QTreeView, and QSortFilterProxyModel

Using QFileSystemModel, QTreeView, and QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 6 Posters 1.6k Views 3 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.
  • Z Offline
    Z Offline
    Zbigniew-Sch
    wrote last edited by
    #1

    Hi,

    I'm using QFileSystemModel, QTreeView, and QSortFilterProxyModel.
    Here's the problem: when I rename a directory and delete a file from that directory, the file is deleted,
    but the corresponding item in QTreeView isn't.
    If I only use QFileSystemModel and QTreeView without QSortFilterProxyModel, everything works correctly.
    Has anyone else encountered this problem, or does anyone have a solution?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zbigniew-Sch
      wrote last edited by
      #30
      1. QLZTreeView - see above
        Header:
      class QLZTreeView : public QTreeView
      {
      	Q_OBJECT
      
      public:
      
      	explicit QLZTreeView(QWidget* parent = nullptr);
      	~QLZTreeView();
      
      protected:
      	void renameCurrentObject(QModelIndex coCurrentIndex);
      	void deleteCurrentObject(QModelIndex coCurrentIndex);
      	void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint) override;
      
      private slots:
      	void customContextMenu(const QPoint& point);
      };
      
      1. QSortFilterProxyModel - is standard Qt class
      2. QLZFileSystemModel - see above
        Header:
          class QLZFileSystemModel : public QFileSystemModel
         {
         Q_OBJECT
        
         public:
         explicit QLZFileSystemModel(QObject* parent = nullptr);
         ~QLZFileSystemModel();
        
         protected:
         Qt::ItemFlags flags(const QModelIndex& index) const override;
        

      };

      1. main.cpp - and that's all.
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          LZImageViewerTest window;
          window.show();
          return app.exec();
      }
      
      
      
      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote last edited by
        #2

        There are a few ways to fix this, but the easiest is to enable dynamicSortFilter

        (Z(:^

        1 Reply Last reply
        1
        • Z Offline
          Z Offline
          Zbigniew-Sch
          wrote last edited by
          #3

          Hi,

          I called "mySortFilterProxyModel->setDynamicSortFilter(false)" but unfortunately the file item is not deleted from the QTreeView.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote last edited by
            #4

            I meant you need to set it to true, but judging from your reply it was already set so (it is the default setting after all).

            Another way to force this functionality is to call QSortFilterProxyModel::invalidate() whenever your base model changes, you can do this by connecting some signals and slots if I recall correctly.

            (Z(:^

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zbigniew-Sch
              wrote last edited by
              #5

              I set it to "true" (but that's the default) and called "invalidate()", but it still doesn't work.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote last edited by
                #6

                Hm, no ideas then. Might as well be a bug of some sorts, although it seems quite unlikely in such an old and well tested class. This is on some desktop platform, with an up-to-date Qt version?

                (Z(:^

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote last edited by
                  #7

                  I think on some old project in a vaguely similar situation, toggling recursiveFilteringEnabled did help me, but I may be misremembering things.

                  (Z(:^

                  1 Reply Last reply
                  1
                  • Z Offline
                    Z Offline
                    Zbigniew-Sch
                    wrote last edited by
                    #8

                    I'm using Windows 11 and Qt 6.10. I think this is a bug, or does anyone have a solution?
                    I've tried various changes, but it doesn't work.

                    JonBJ 1 Reply Last reply
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote last edited by
                      #9

                      It would help to have a minimal working example program to look at. This functions for me as I expect with Qt 6.9 on macOS:

                      #include <QApplication>
                      #include <QFileSystemModel>
                      #include <QSortFilterProxyModel>
                      #include <QTreeView>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          QFileSystemModel model;
                          model.setRootPath("/tmp");
                      
                          QSortFilterProxyModel proxyModel;
                          proxyModel.setSourceModel(&model);
                      
                          QTreeView view;
                          view.setModel(&proxyModel);
                          view.show();
                      
                          return a.exec();
                      }
                      

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      3
                      • Z Offline
                        Z Offline
                        Zbigniew-Sch
                        wrote last edited by
                        #10

                        Sorry, but you didn't read the question. It's about the following: renaming a directory and
                        deleting a file from that directory, but the file item isn't being deleted from QTreeView.

                        JonBJ jeremy_kJ 2 Replies Last reply
                        0
                        • Z Zbigniew-Sch

                          Sorry, but you didn't read the question. It's about the following: renaming a directory and
                          deleting a file from that directory, but the file item isn't being deleted from QTreeView.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote last edited by JonB
                          #11

                          @Zbigniew-Sch
                          You still need to provide a minimal example of code you use which shows this behaviour:

                          • We need to see exactly what your code is.
                          • If someone wants to test behaviour why should they have to write code from scratch? (I did read your question yesterday, but no code => I'm not going to try anything.)

                          So... can you confirm that if you try precisely the code in @jeremy_k's code the problem arises for you? Assuming so, then: as I understand it, you are performing a directory rename and then a delete file within the renamed directory, both of these actions from the QTreeView? (You are not doing these file operations externally to your program on the file system, are you?) So someone else needs to do that while running the same code as you. I don't know whether the QTreeView bound to a file system offers you "rename" and "delete" automatically, but if not then we need you add whatever minimal for that to the code for people to try.

                          1 Reply Last reply
                          2
                          • Z Zbigniew-Sch

                            Sorry, but you didn't read the question. It's about the following: renaming a directory and
                            deleting a file from that directory, but the file item isn't being deleted from QTreeView.

                            jeremy_kJ Offline
                            jeremy_kJ Offline
                            jeremy_k
                            wrote last edited by
                            #12

                            @Zbigniew-Sch said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                            Sorry, but you didn't read the question. It's about the following: renaming a directory and
                            deleting a file from that directory, but the file item isn't being deleted from QTreeView.

                            Yes, I did read the question. The MWE I posted behaves as I expect, given an out-of-process directory renaming and file deletion.

                            There may be a bug. Without a program to examine, it's unclear whether it is likely to lie in the OS, library, application code, or understanding of how each behaves.

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            JonBJ 1 Reply Last reply
                            1
                            • jeremy_kJ jeremy_k

                              @Zbigniew-Sch said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                              Sorry, but you didn't read the question. It's about the following: renaming a directory and
                              deleting a file from that directory, but the file item isn't being deleted from QTreeView.

                              Yes, I did read the question. The MWE I posted behaves as I expect, given an out-of-process directory renaming and file deletion.

                              There may be a bug. Without a program to examine, it's unclear whether it is likely to lie in the OS, library, application code, or understanding of how each behaves.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote last edited by JonB
                              #13

                              @jeremy_k said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                              given an out-of-process directory renaming and file deletion

                              Oh, I had thought it more likely OP was doing in-process operations via code on the QTreeView! Shows how simple details matter :)

                              jeremy_kJ 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @jeremy_k said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                given an out-of-process directory renaming and file deletion

                                Oh, I had thought it more likely OP was doing in-process operations via code on the QTreeView! Shows how simple details matter :)

                                jeremy_kJ Offline
                                jeremy_kJ Offline
                                jeremy_k
                                wrote last edited by
                                #14

                                @JonB said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                @jeremy_k said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                given an out-of-process directory renaming and file deletion

                                Oh, I had thought it more likely OP was doing in-process operations via code on the QTreeView! Shows how simple details matter :)

                                +1 on details mattering. I didn't see a point in guessing and complicating the code for an example of a MWE.

                                Asking a question about code? http://eel.is/iso-c++/testcase/

                                JonBJ 1 Reply Last reply
                                0
                                • jeremy_kJ jeremy_k

                                  @JonB said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                  @jeremy_k said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                  given an out-of-process directory renaming and file deletion

                                  Oh, I had thought it more likely OP was doing in-process operations via code on the QTreeView! Shows how simple details matter :)

                                  +1 on details mattering. I didn't see a point in guessing and complicating the code for an example of a MWE.

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote last edited by JonB
                                  #15

                                  @jeremy_k
                                  OP is Windows, you are MacOS. I am Linux but not tested. May have to await a Windows user. If it's out-of-process it relies on the QFileSystemWatcher and the OS behaviour for notifications, that might or might not be relevant. If it's that area OP could debug-out to confirm the QFSW signals being emitted.

                                  If it's a fault in higher-level Qt logic instead we should be able to reproduce, but OP may have to confirm exactly what he does in outside world.

                                  And if it is an issue only when there is a QSortFilterProxyModel we need to know whether OP has set any sorting or filtering on it.

                                  jeremy_kJ 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @jeremy_k
                                    OP is Windows, you are MacOS. I am Linux but not tested. May have to await a Windows user. If it's out-of-process it relies on the QFileSystemWatcher and the OS behaviour for notifications, that might or might not be relevant. If it's that area OP could debug-out to confirm the QFSW signals being emitted.

                                    If it's a fault in higher-level Qt logic instead we should be able to reproduce, but OP may have to confirm exactly what he does in outside world.

                                    And if it is an issue only when there is a QSortFilterProxyModel we need to know whether OP has set any sorting or filtering on it.

                                    jeremy_kJ Offline
                                    jeremy_kJ Offline
                                    jeremy_k
                                    wrote last edited by
                                    #16

                                    @JonB said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                    If it's out-of-process it relies on the QFileSystemWatcher and the OS behaviour for notifications, that might or might not be relevant.

                                    This seems unlikely to be relevant. The original post says that connecting the file system model directly to the view does what is expected. I don't expect to find any file or operating system specific code in the proxy model.

                                    Asking a question about code? http://eel.is/iso-c++/testcase/

                                    1 Reply Last reply
                                    0
                                    • Z Offline
                                      Z Offline
                                      Zbigniew-Sch
                                      wrote last edited by
                                      #17

                                      I wrote a small program and I'm having the same problem.
                                      This is definitely a bug in Qt, without QSortFilterProxyModel, everything works correctly.
                                      I renamed the directory in my program and deleted a file from that directory
                                      using Windows Explorer – it works correctly now. I'm out of ideas.

                                      1 Reply Last reply
                                      0
                                      • jeremy_kJ Offline
                                        jeremy_kJ Offline
                                        jeremy_k
                                        wrote last edited by
                                        #18

                                        The general advice from this thread remains valid:

                                        • The existence of a test program doesn't help confirm or deny if nobody else can see the program.
                                        • Bug reports can be filed at https://bugreports.qt.io/. Chances are high that whoever responds will also ask for a demonstrator.

                                        Asking a question about code? http://eel.is/iso-c++/testcase/

                                        Christian EhrlicherC 1 Reply Last reply
                                        0
                                        • jeremy_kJ jeremy_k

                                          The general advice from this thread remains valid:

                                          • The existence of a test program doesn't help confirm or deny if nobody else can see the program.
                                          • Bug reports can be filed at https://bugreports.qt.io/. Chances are high that whoever responds will also ask for a demonstrator.
                                          Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote last edited by
                                          #19

                                          @jeremy_k said in Using QFileSystemModel, QTreeView, and QSortFilterProxyModel:

                                          Chances are high that whoever responds will also ask for a demonstrator.

                                          Higher than high - otherwise it will be closed sooner or later with 'Needs more information'

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          0
                                          • Z Offline
                                            Z Offline
                                            Zbigniew-Sch
                                            wrote last edited by
                                            #20

                                            How can I upload an entire example project as a ZIP file?

                                            JonBJ 1 Reply Last reply
                                            0

                                            • Login

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