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. QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.
Forum Updated to NodeBB v4.3 + New Features

QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 893 Views 1 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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by
    #1

    Hi.
    I would like to use QFileSystemWatcher in Qt 5.15.2 to watch a directory and notify the user when files are deleted outside the app. When that happens, I would like to display a message to the user, asking him/her to reload a particular list based on the directory's file content.

    The problem I am facing is that the directoryChanged(QString) signal seems to be emitted once for each file deleted, even if those files were deleted "at the same time" by the user.

    How do I go about if I want to display the "Directory has changed" message only once in that case?

    Thanks for your suggestions!

    Christian EhrlicherC 1 Reply Last reply
    0
    • DiracsbracketD Diracsbracket

      Hi.
      I would like to use QFileSystemWatcher in Qt 5.15.2 to watch a directory and notify the user when files are deleted outside the app. When that happens, I would like to display a message to the user, asking him/her to reload a particular list based on the directory's file content.

      The problem I am facing is that the directoryChanged(QString) signal seems to be emitted once for each file deleted, even if those files were deleted "at the same time" by the user.

      How do I go about if I want to display the "Directory has changed" message only once in that case?

      Thanks for your suggestions!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

      even if those files were deleted "at the same time" by the user.

      These are single changes in the filesystem too.

      How do I go about if I want to display the "Directory has changed" message only once in that case?

      Maybe wait for s small period - if more deletion signals come in you can show another message to the user.

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

      DiracsbracketD 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

        even if those files were deleted "at the same time" by the user.

        These are single changes in the filesystem too.

        How do I go about if I want to display the "Directory has changed" message only once in that case?

        Maybe wait for s small period - if more deletion signals come in you can show another message to the user.

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        Hi Christian,
        @Christian-Ehrlicher said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

        These are single changes in the filesystem too.

        What I am doing is select all the files in the folder in Windows Explorer, and deleting them at the same time. One signal seems to be generated for every file deleted, so the system does not seem to consider it as a single change.

        @Christian-Ehrlicher said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

        Maybe wait for s small period - if more deletion signals come in you can show another message to the user.

        I was hoping for a more robust way though...

        JonBJ 1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          Hi @Diracsbracket,

          How do I go about if I want to display the "Directory has changed" message only once in that case?
          ...
          I was hoping for a more robust way though...

          There's two ways I can think of. Either:

          1. In the slot function, temporarily disable the watcher (eg disconnect the signal), then after the message has been displayed, re-enable it; or
          2. In the slot function first check some state to see if the message is already showing, and if not, then show the message, otherwise ignore it.

          That said, a good example to look at would be Qt's own Qt Creator. It essentially does #2, by showing a modal dialog, then the slot just checks if any modal dialog is showing. Note, this has the advantage of also not interrupting other modal dialogs, that might be unrelated to the filesystem changes. But of course, that only works if the reload prompt is modal - you might be showing non-modal, or a status bar etc, in which case use a simple member variable or something similar as appropriate.

          Have a look at documentmanager.cpp, especially the DocumentManager::checkForReload() function for inspiration.

          Cheers.

          DiracsbracketD 1 Reply Last reply
          1
          • DiracsbracketD Diracsbracket

            Hi Christian,
            @Christian-Ehrlicher said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

            These are single changes in the filesystem too.

            What I am doing is select all the files in the folder in Windows Explorer, and deleting them at the same time. One signal seems to be generated for every file deleted, so the system does not seem to consider it as a single change.

            @Christian-Ehrlicher said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

            Maybe wait for s small period - if more deletion signals come in you can show another message to the user.

            I was hoping for a more robust way though...

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

            @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

            I was hoping for a more robust way though...

            I don't understand what more "robust way" you are expecting to find. I am pretty sure that when, say, a user selects a bunch of files in Explorer and deletes them, Explorer issues system calls to delete them one at a time. I don't think there is any "delete these multiple files" system call in Windows (or Linux), nor does Explorer issue any "begin/end multiple delete" notification.

            So what is the difference between, say, the user selecting two files and deleting them versus selecting one, deleting it, then selecting another and deleting that? I assume nothing other than the timing.

            Given that, you can do what you want in any of the multiple ways suggested which in some shape or form look at timing to decide how many/when to show dialogs to the user. That might be e.g. set a single shot timer from the first delete and don't report till it expires, optionally renewing the timer if more deletes come in during its period, or it might be show a dialog immediately on the first one and then look at the time when the next one arrives and ignore it if "close" to the first time, or some "ignore further notifications while dialog is presented" in whatever shape or form.

            DiracsbracketD 1 Reply Last reply
            1
            • JonBJ JonB

              @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

              I was hoping for a more robust way though...

              I don't understand what more "robust way" you are expecting to find. I am pretty sure that when, say, a user selects a bunch of files in Explorer and deletes them, Explorer issues system calls to delete them one at a time. I don't think there is any "delete these multiple files" system call in Windows (or Linux), nor does Explorer issue any "begin/end multiple delete" notification.

              So what is the difference between, say, the user selecting two files and deleting them versus selecting one, deleting it, then selecting another and deleting that? I assume nothing other than the timing.

              Given that, you can do what you want in any of the multiple ways suggested which in some shape or form look at timing to decide how many/when to show dialogs to the user. That might be e.g. set a single shot timer from the first delete and don't report till it expires, optionally renewing the timer if more deletes come in during its period, or it might be show a dialog immediately on the first one and then look at the time when the next one arrives and ignore it if "close" to the first time, or some "ignore further notifications while dialog is presented" in whatever shape or form.

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by
              #6

              @JonB said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

              I don't understand what more "robust way" you are expecting to find. I am pretty sure that when, say, a user selects a bunch of files in Explorer and deletes them, Explorer issues system calls to delete them one at a time.

              I got the fact that the system deletes the files one by one. By "more robust" I meant the way the app/user gets notified of these changes. To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me if the files were deleted as a single block by the user.

              Maybe I should have used the term "elegant" instead of "robust"...

              JonBJ 1 Reply Last reply
              0
              • Paul ColbyP Paul Colby

                Hi @Diracsbracket,

                How do I go about if I want to display the "Directory has changed" message only once in that case?
                ...
                I was hoping for a more robust way though...

                There's two ways I can think of. Either:

                1. In the slot function, temporarily disable the watcher (eg disconnect the signal), then after the message has been displayed, re-enable it; or
                2. In the slot function first check some state to see if the message is already showing, and if not, then show the message, otherwise ignore it.

                That said, a good example to look at would be Qt's own Qt Creator. It essentially does #2, by showing a modal dialog, then the slot just checks if any modal dialog is showing. Note, this has the advantage of also not interrupting other modal dialogs, that might be unrelated to the filesystem changes. But of course, that only works if the reload prompt is modal - you might be showing non-modal, or a status bar etc, in which case use a simple member variable or something similar as appropriate.

                Have a look at documentmanager.cpp, especially the DocumentManager::checkForReload() function for inspiration.

                Cheers.

                DiracsbracketD Offline
                DiracsbracketD Offline
                Diracsbracket
                wrote on last edited by Diracsbracket
                #7

                @Paul-Colby
                Thanks for your suggestions! I'll have a look at DocumentManager.

                I was also thinking something along the lines JonB suggested:

                1. When the first signal arrives for the watched folder, start a timer.

                2. If another signal arrives for the watched folder before the timer expires, then restart the timer.

                3. Only show the dialog to the user when the timer expires.

                With this approach, assuming a suitable timeout period can be set, the user will only be notified once the system has fired all its signals. I am here mainly concerned with the case a large number of files are added/deleted.

                Can you see any disadvantages with this approach?

                Cheers!

                1 Reply Last reply
                0
                • DiracsbracketD Diracsbracket

                  @JonB said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                  I don't understand what more "robust way" you are expecting to find. I am pretty sure that when, say, a user selects a bunch of files in Explorer and deletes them, Explorer issues system calls to delete them one at a time.

                  I got the fact that the system deletes the files one by one. By "more robust" I meant the way the app/user gets notified of these changes. To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me if the files were deleted as a single block by the user.

                  Maybe I should have used the term "elegant" instead of "robust"...

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

                  @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                  To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me if the files were deleted as a single block by the user.

                  I agree that I would not pick the approach which delays notifying the user in case more deletes come in: so pick the approach where you notify the user immediately on first delete, and decide how you would like to handle further deletes which arrive "shortly thereafter", which you take to be a "single block".

                  As I said, Explorer/the OS sends separate notifications. You could look for further information maybe in the Windows SDK shellapi.h file or IFileOperation::DeleteItems method for Win-specific stuff. Explorer may use these. I do not know whether Windows offers "notifications" for such an action which you could subscribe to.

                  In your latest post you write:

                  Only show the dialog to the user when the timer expires.

                  which is exactly the opposite of what you replied to me

                  To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me

                  Or maybe I misunderstand just what you are saying. In any case, you have a choice between

                  • Notify user immediately when first file deleted, think about what you want to do if more delete notifications arrive immediately after this.
                  • Delay notifying user till a time period has past, in case more notifications arrive.
                  DiracsbracketD 2 Replies Last reply
                  0
                  • JonBJ JonB

                    @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                    To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me if the files were deleted as a single block by the user.

                    I agree that I would not pick the approach which delays notifying the user in case more deletes come in: so pick the approach where you notify the user immediately on first delete, and decide how you would like to handle further deletes which arrive "shortly thereafter", which you take to be a "single block".

                    As I said, Explorer/the OS sends separate notifications. You could look for further information maybe in the Windows SDK shellapi.h file or IFileOperation::DeleteItems method for Win-specific stuff. Explorer may use these. I do not know whether Windows offers "notifications" for such an action which you could subscribe to.

                    In your latest post you write:

                    Only show the dialog to the user when the timer expires.

                    which is exactly the opposite of what you replied to me

                    To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me

                    Or maybe I misunderstand just what you are saying. In any case, you have a choice between

                    • Notify user immediately when first file deleted, think about what you want to do if more delete notifications arrive immediately after this.
                    • Delay notifying user till a time period has past, in case more notifications arrive.
                    DiracsbracketD Offline
                    DiracsbracketD Offline
                    Diracsbracket
                    wrote on last edited by Diracsbracket
                    #9

                    @JonB said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                    which is exactly the opposite of what you replied to me

                    I edited my previous message to acknowledge you suggested that approach in your reply, which I hadn't read thoroughly.

                    That said, the point I was trying to make is that I didn't like the fact of showing a message, then wait for some time, and then possibly show another message for the same block operation. I was hoping that by choosing an appropriate timeout period, such second message would never be shown.

                    JonBJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                      To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me if the files were deleted as a single block by the user.

                      I agree that I would not pick the approach which delays notifying the user in case more deletes come in: so pick the approach where you notify the user immediately on first delete, and decide how you would like to handle further deletes which arrive "shortly thereafter", which you take to be a "single block".

                      As I said, Explorer/the OS sends separate notifications. You could look for further information maybe in the Windows SDK shellapi.h file or IFileOperation::DeleteItems method for Win-specific stuff. Explorer may use these. I do not know whether Windows offers "notifications" for such an action which you could subscribe to.

                      In your latest post you write:

                      Only show the dialog to the user when the timer expires.

                      which is exactly the opposite of what you replied to me

                      To "wait for a small time" and then display another message if more signals come in did not seem satisfactory to me

                      Or maybe I misunderstand just what you are saying. In any case, you have a choice between

                      • Notify user immediately when first file deleted, think about what you want to do if more delete notifications arrive immediately after this.
                      • Delay notifying user till a time period has past, in case more notifications arrive.
                      DiracsbracketD Offline
                      DiracsbracketD Offline
                      Diracsbracket
                      wrote on last edited by
                      #10

                      @JonB
                      Apologies for my messy, hasty postings. I need to take more time reading your replies correctly...

                      @JonB said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                      I agree that I would not pick the approach which delays notifying the user in case more deletes come in: so pick the approach where you notify the user immediately on first delete, and decide how you would like to handle further deletes which arrive "shortly thereafter", which you take to be a "single block".

                      So, you think the approach of delaying the notification to the user until the (hopefully) last signal is received is not a good one?

                      1 Reply Last reply
                      0
                      • DiracsbracketD Diracsbracket

                        @JonB said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                        which is exactly the opposite of what you replied to me

                        I edited my previous message to acknowledge you suggested that approach in your reply, which I hadn't read thoroughly.

                        That said, the point I was trying to make is that I didn't like the fact of showing a message, then wait for some time, and then possibly show another message for the same block operation. I was hoping that by choosing an appropriate timeout period, such second message would never be shown.

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

                        @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                        I didn't like the fact of showing a message, then wait for some time, and then possibly show another message for the same block operation. I was hoping that by choosing an appropriate timeout period, such second message would never be shown.

                        I agree that it will be "irritating" to show a box to the user, have them dismiss it, and then show another box for further deletions!

                        This all comes down to you time periods for delaying.

                        You will have a potential problem if you show a modal information message box. Have you considered whether a modeless window/dialog would suit you/your user? That can be updated as soon as deletes arrive, whenever they are. I don't know what your application is which wants to show a modal box when deletes happen, it could "interrupt" me as a user and suffers from the potential of being shown too frequently as we are discussing.

                        DiracsbracketD 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Diracsbracket said in QFileSystemWatcher: handle onFileSystemChanged() signal only once when multiple files are deleted at the same time.:

                          I didn't like the fact of showing a message, then wait for some time, and then possibly show another message for the same block operation. I was hoping that by choosing an appropriate timeout period, such second message would never be shown.

                          I agree that it will be "irritating" to show a box to the user, have them dismiss it, and then show another box for further deletions!

                          This all comes down to you time periods for delaying.

                          You will have a potential problem if you show a modal information message box. Have you considered whether a modeless window/dialog would suit you/your user? That can be updated as soon as deletes arrive, whenever they are. I don't know what your application is which wants to show a modal box when deletes happen, it could "interrupt" me as a user and suffers from the potential of being shown too frequently as we are discussing.

                          DiracsbracketD Offline
                          DiracsbracketD Offline
                          Diracsbracket
                          wrote on last edited by Diracsbracket
                          #12

                          @JonB
                          Indeed, maybe I am just complicating things too much by wanting to display a (modal) dialog. It is probably just simpler to just give a visual indication to the user somewhere in the View/List concerned that changes occurred to the directory. That should be enough to prompt the user to reload the View/List. This is equivalent to your non-modal dialog suggestion, I suppose.

                          At long last, I am starting to see the light... which is exactly why I am coming to this forum.

                          Thanks!

                          JonBJ 1 Reply Last reply
                          0
                          • DiracsbracketD Diracsbracket

                            @JonB
                            Indeed, maybe I am just complicating things too much by wanting to display a (modal) dialog. It is probably just simpler to just give a visual indication to the user somewhere in the View/List concerned that changes occurred to the directory. That should be enough to prompt the user to reload the View/List. This is equivalent to your non-modal dialog suggestion, I suppose.

                            At long last, I am starting to see the light... which is exactly why I am coming to this forum.

                            Thanks!

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

                            @Diracsbracket
                            Yes, it depends on your application/goal. But the thought for me as a user that while I am using your app I am liable to be "stopped" by the presentation of modal dialogs, potentially multiple times whether for a single block delete or at least for separate deletes even if they do not form a block, is not "appealing". Can it be in your software that I am, say, typing into an edit control and for whatever reason a file gets deleted and my typing gets interrupted by a modal dialog popping up? I don't like that :)

                            DiracsbracketD 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @Diracsbracket
                              Yes, it depends on your application/goal. But the thought for me as a user that while I am using your app I am liable to be "stopped" by the presentation of modal dialogs, potentially multiple times whether for a single block delete or at least for separate deletes even if they do not form a block, is not "appealing". Can it be in your software that I am, say, typing into an edit control and for whatever reason a file gets deleted and my typing gets interrupted by a modal dialog popping up? I don't like that :)

                              DiracsbracketD Offline
                              DiracsbracketD Offline
                              Diracsbracket
                              wrote on last edited by
                              #14

                              @JonB
                              I might just turn the View/List into a red, blinking one. That should be enough of a notification to the user. :-)

                              JonBJ 1 Reply Last reply
                              0
                              • DiracsbracketD Diracsbracket

                                @JonB
                                I might just turn the View/List into a red, blinking one. That should be enough of a notification to the user. :-)

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

                                @Diracsbracket
                                I think that would be a whole lot simpler for this multi-file-delete notification! :)

                                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