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. QFileSystemModel::setRootPath behavior does not match documentation under Windows

QFileSystemModel::setRootPath behavior does not match documentation under Windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 318 Views
  • 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.
  • J Jo Jo
    29 Mar 2025, 10:16

    The Qt documentation says about QFileSystemModel::setRootPath():

    Sets the directory that is being watched by the model to newPath by installing a file system watcher on it. Any changes to files and directories within this directory will be reflected in the model.

    https://doc.qt.io/qt-6/qfilesystemmodel.html#setRootPath

    Lets say I use setRootPath in the following way:

    auto model = new QFileSystemModel;
    model->setRootPath("C:\\folder");
    treeView->setModel(model);
    

    According to the documentation, the model should now track changes in the specified path C:\folder. However, the model also tracks changes outside of C:\folder. Changes are tracked even on other disks.

    Why is this happening? How to correctly understand the work of this method? If QFileSystemModel monitors all paths that are visible in QTreeView, then why does the model require a specific path to be monitored? After all, all paths are monitored anyway.

    J Offline
    J Offline
    JonB
    wrote on 29 Mar 2025, 12:02 last edited by
    #2

    @Jo-Jo
    You can find out about this by Google (e.g. qfilesystemmodel setrootpath).

    I think you will find that e.g. https://stackoverflow.com/a/49617341/489865 deals with this:

    You must indicate to the QTreeView what is your root item with setRootIndex():

            model.setRootPath(QDir.rootPath())
            self.setRootIndex(model.index("C:"))
    

    So you have to the QTreeView::setRootIndex() as well as the QFileSystemModel::setRootPath() ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jo Jo
      wrote on 29 Mar 2025, 12:10 last edited by Jo Jo
      #3

      @JonB Thank you, but I'm not asking about what the view shows. The question is, if all paths that are visible in QTreeView are monitored by the model anyway, why do you need to use setRootPath to set the monitoring path? After all, all paths are monitored anyway after setRootPath

      J 1 Reply Last reply 29 Mar 2025, 13:34
      0
      • J Jo Jo
        29 Mar 2025, 12:10

        @JonB Thank you, but I'm not asking about what the view shows. The question is, if all paths that are visible in QTreeView are monitored by the model anyway, why do you need to use setRootPath to set the monitoring path? After all, all paths are monitored anyway after setRootPath

        J Offline
        J Offline
        JonB
        wrote on 29 Mar 2025, 13:34 last edited by JonB
        #4

        @Jo-Jo
        Yes, I understand.

        First, just in case, verify that your model->setRootPath("C:\\folder") returns a valid QModelIndex and try with C:/folder instead just in case. To get that out of the way.

        Then see QFileSystemModel::setRootPath behavior does not seems to match the documentation to see that you are not alone in this question and, from what I can see, nobody knows why/the exact behaviour! They just care about getting their QTreeView showing the right stuff :) It might be that your behaviour is Windows-only, like I said before monitoring code depends on platform offerings and what underlying call or implementation that and Qt use. You could try to see how the root path is actually used in the Windows code.

        1 Reply Last reply
        1
        • J Offline
          J Offline
          Jo Jo
          wrote on 29 Mar 2025, 13:57 last edited by
          #5

          @JonB OK, thank you! :)

          1 Reply Last reply
          0
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 29 Mar 2025, 14:13 last edited by
            #6

            How do you know that other paths are also tracked?
            The internal object for sure only looks in the root path specified.

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

            J 1 Reply Last reply 29 Mar 2025, 14:27
            0
            • C Christian Ehrlicher
              29 Mar 2025, 14:13

              How do you know that other paths are also tracked?
              The internal object for sure only looks in the root path specified.

              J Offline
              J Offline
              JonB
              wrote on 29 Mar 2025, 14:27 last edited by
              #7

              @Christian-Ehrlicher
              That sounds more like it! But remember the stackoverflow post also claims:

              The whole file system is being watched. Even if I make a change in E drive I can notice the same getting reflected in the view (model). This behavior seems to be different from the documentation.

              1 Reply Last reply
              0
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 29 Mar 2025, 14:30 last edited by
                #8

                How do you see a change when the view does not show this path?

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

                J 1 Reply Last reply 29 Mar 2025, 14:54
                0
                • C Christian Ehrlicher
                  29 Mar 2025, 14:30

                  How do you see a change when the view does not show this path?

                  J Offline
                  J Offline
                  Jo Jo
                  wrote on 29 Mar 2025, 14:54 last edited by
                  #9

                  @Christian-Ehrlicher said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:

                  How do you see a change when the view does not show this path?

                  I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:

                  If you set C: as setRootPath, then QTreeView will show changes in both C: and D:

                  C 1 Reply Last reply 29 Mar 2025, 16:03
                  0
                  • J Jo Jo
                    29 Mar 2025, 14:54

                    @Christian-Ehrlicher said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:

                    How do you see a change when the view does not show this path?

                    I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:

                    If you set C: as setRootPath, then QTreeView will show changes in both C: and D:

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 29 Mar 2025, 16:03 last edited by
                    #10

                    @Jo-Jo said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:

                    I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:

                    If you set C: as setRootPath, then QTreeView will show changes in both C: and D:

                    I don't understand - when you set C:\ as the root path why/how to you see the contents of D:\ ?

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

                    J 1 Reply Last reply 29 Mar 2025, 17:25
                    0
                    • C Christian Ehrlicher
                      29 Mar 2025, 16:03

                      @Jo-Jo said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:

                      I am talking about the situation when the view shows the contents of a folder. For example, there are two disks: 1) C: and 2) D:

                      If you set C: as setRootPath, then QTreeView will show changes in both C: and D:

                      I don't understand - when you set C:\ as the root path why/how to you see the contents of D:\ ?

                      J Offline
                      J Offline
                      Jo Jo
                      wrote on 29 Mar 2025, 17:25 last edited by
                      #11

                      @Christian-Ehrlicher said in QFileSystemModel::setRootPath behavior does not match documentation under Windows:

                      I don't understand - when you set C:\ as the root path why/how to you see the contents of D:\ ?

                      I think you are confusing QFileSystemModel::setRootPath and QTreeView::setRootIndex

                      1 Reply Last reply
                      0

                      11/11

                      29 Mar 2025, 17:25

                      • Login

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