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. Updating QFileSystemModel when inserting a flash card (Windows)
Qt 6.11 is out! See what's new in the release blog

Updating QFileSystemModel when inserting a flash card (Windows)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.3k 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 Offline
    J Offline
    Jo Jo
    wrote on last edited by Jo Jo
    #1

    I am using QFileSystemModel together with QTreeView. I want QTreeView to update its state and show the flash card in the tree when I insert a flash drive into the computer.

    Documentation for QFileSystemModel::setRootPath says:

    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.

    What specific path should be set in setRootPath to track changes when a flash drive is inserted? Here is my code

    #include <QApplication>
    #include <QTreeView>
    #include <QFileSystemModel>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        QFileSystemModel *model = new QFileSystemModel;
        model->setRootPath("???");
        model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::NoSymLinks);
    
        QTreeView *treeView = new QTreeView;
        treeView->setModel(model);
    
        treeView->setColumnWidth(0, 250);
        treeView->setWindowTitle("QFileSystemModel Example");
        treeView->resize(600, 400);
    
        treeView->show();
    
        return app.exec();
    }
    
    JonBJ 1 Reply Last reply
    0
    • J Jo Jo

      I am using QFileSystemModel together with QTreeView. I want QTreeView to update its state and show the flash card in the tree when I insert a flash drive into the computer.

      Documentation for QFileSystemModel::setRootPath says:

      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.

      What specific path should be set in setRootPath to track changes when a flash drive is inserted? Here is my code

      #include <QApplication>
      #include <QTreeView>
      #include <QFileSystemModel>
      
      int main(int argc, char *argv[]) {
          QApplication app(argc, argv);
      
          QFileSystemModel *model = new QFileSystemModel;
          model->setRootPath("???");
          model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::NoSymLinks);
      
          QTreeView *treeView = new QTreeView;
          treeView->setModel(model);
      
          treeView->setColumnWidth(0, 250);
          treeView->setWindowTitle("QFileSystemModel Example");
          treeView->resize(600, 400);
      
          treeView->show();
      
          return app.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Jo-Jo said in Updating QFileSystemModel when inserting a flash card (Windows):

      What specific path should be set in setRootPath to track changes when a flash drive is inserted?

      If anything works at all it would depend on your OS and where the drive gets mounted, about which we know nothing.

      If you can find something which works for you on your OS that is great. But it would not surprise me if you have trouble seeing this situation. Ultimately if I wanted to check behaviour for QFileSystem notifications I would look at the Qt source for your platform (as you did for your QFileSystemModel question), as the implementation varies and relies on whatever the underlying OS call does or does not support.

      J 1 Reply Last reply
      0
      • JonBJ JonB

        @Jo-Jo said in Updating QFileSystemModel when inserting a flash card (Windows):

        What specific path should be set in setRootPath to track changes when a flash drive is inserted?

        If anything works at all it would depend on your OS and where the drive gets mounted, about which we know nothing.

        If you can find something which works for you on your OS that is great. But it would not surprise me if you have trouble seeing this situation. Ultimately if I wanted to check behaviour for QFileSystem notifications I would look at the Qt source for your platform (as you did for your QFileSystemModel question), as the implementation varies and relies on whatever the underlying OS call does or does not support.

        J Offline
        J Offline
        Jo Jo
        wrote on last edited by Jo Jo
        #3

        @JonB said in Updating QFileSystemModel when inserting a flash card (Windows):

        If anything works at all it would depend on your OS and where the drive gets mounted, about which we know nothing.

        I am using Windows 11. I'm trying to find information in the source code but so far without success. As i understand, it uses QWindowsFileSystemWatcherEngine and i can`t found this class in sources

        JonBJ 1 Reply Last reply
        0
        • J Jo Jo

          @JonB said in Updating QFileSystemModel when inserting a flash card (Windows):

          If anything works at all it would depend on your OS and where the drive gets mounted, about which we know nothing.

          I am using Windows 11. I'm trying to find information in the source code but so far without success. As i understand, it uses QWindowsFileSystemWatcherEngine and i can`t found this class in sources

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Jo-Jo
          I am a Linux user rather than Windows. I believe the code will be in some Windows-specific Qt source file. I believe the base system call used will be something like FindFirstChangeNotification(), per e.g. https://learn.microsoft.com/en-us/windows/win32/fileio/obtaining-directory-change-notifications. You might be able to find that in source code? I do not know whether Windows has different calls for top-level removable media,

          Hmm, it might get more complicated. There is also a WM_DEVICECHANGE Windows message. See maybe https://learn.microsoft.com/en-us/windows/win32/devio/detecting-media-insertion-or-removal. And then there is SHChangeNotifyRegister....

          I think class QWindowsFileSystemWatcherEngine : public QFileSystemWatcherEngine might be in in a header file, named qfilesystemwatcher_win_p.h, somewhere?

          You may have to do some Googling --- good luck!

          J 1 Reply Last reply
          0
          • JonBJ JonB

            @Jo-Jo
            I am a Linux user rather than Windows. I believe the code will be in some Windows-specific Qt source file. I believe the base system call used will be something like FindFirstChangeNotification(), per e.g. https://learn.microsoft.com/en-us/windows/win32/fileio/obtaining-directory-change-notifications. You might be able to find that in source code? I do not know whether Windows has different calls for top-level removable media,

            Hmm, it might get more complicated. There is also a WM_DEVICECHANGE Windows message. See maybe https://learn.microsoft.com/en-us/windows/win32/devio/detecting-media-insertion-or-removal. And then there is SHChangeNotifyRegister....

            I think class QWindowsFileSystemWatcherEngine : public QFileSystemWatcherEngine might be in in a header file, named qfilesystemwatcher_win_p.h, somewhere?

            You may have to do some Googling --- good luck!

            J Offline
            J Offline
            Jo Jo
            wrote on last edited by
            #5

            @JonB said in Updating QFileSystemModel when inserting a flash card (Windows):

            WM_DEVICECHANGE

            This only works when inserting and removing the flash drive, but this is not enough. After inserting the flash drive, it is not immediately available in the file system, some time must pass. Also, this message does not come when the user presses Eject.

            You may have to do some Googling --- good luck!

            Thank you!:)

            JonBJ 1 Reply Last reply
            0
            • J Jo Jo

              @JonB said in Updating QFileSystemModel when inserting a flash card (Windows):

              WM_DEVICECHANGE

              This only works when inserting and removing the flash drive, but this is not enough. After inserting the flash drive, it is not immediately available in the file system, some time must pass. Also, this message does not come when the user presses Eject.

              You may have to do some Googling --- good luck!

              Thank you!:)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Jo-Jo said in Updating QFileSystemModel when inserting a flash card (Windows):

              This only works when inserting and removing the flash drive, but this is not enough. After inserting the flash drive, it is not immediately available in the file system, some time must pass.

              Indeed! You wish to look at things from the file system level which is separate from this more "physical" level, I was aware of this.

              I hate to say this, but you might see whether ChatGPT has an answer for your case!

              J 1 Reply Last reply
              0
              • JonBJ JonB

                @Jo-Jo said in Updating QFileSystemModel when inserting a flash card (Windows):

                This only works when inserting and removing the flash drive, but this is not enough. After inserting the flash drive, it is not immediately available in the file system, some time must pass.

                Indeed! You wish to look at things from the file system level which is separate from this more "physical" level, I was aware of this.

                I hate to say this, but you might see whether ChatGPT has an answer for your case!

                J Offline
                J Offline
                Jo Jo
                wrote on last edited by
                #7

                @JonB said in Updating QFileSystemModel when inserting a flash card (Windows):

                Indeed! You wish to look at things from the file system level which is separate from this more "physical" level, I was aware of this.

                I hate to say this, but you might see whether ChatGPT has an answer for your case!

                I'll try, thank you!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jo Jo
                  wrote on last edited by
                  #8

                  I found out that flash drive detection is almost independent of setRootPath. If you set any correct path in setRootPath, then when you insert a flash drive, QTreeView will display it.

                  On the other hand, the behavior of setRootPath still remains unclear. If, for example, I set setRootPath like this:

                  model->setRootPath("C:\Users\Username\Desktop\folder");

                  Even though C:\Users\Username\Desktop\folder was set as the path to be monitored, QFileSystemModel monitors changes in other directories as well. For example, if you create a file in C:, QTreeView will display this file despite the fact that we are monitoring C:\Users\Username\Desktop\folder and not C:\

                  What is the reason for this behavior, maybe someone knows?

                  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