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. Copy of large files and QFileSystemWatcher under windows
Forum Update on Monday, May 27th 2025

Copy of large files and QFileSystemWatcher under windows

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 7.4k 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.
  • S Offline
    S Offline
    stephane78
    wrote on 20 Jan 2017, 11:01 last edited by VRonin
    #1

    Hi,
    I have already signaled the QtBug 52226 about renaming of subdirectories with QFileSystemWatcher.
    I have another problem when I copy a large file (300 Mo or 1 Go) into a directory watched by the QFileSystemWtacher. The problem is that the signal fileChanged is emitted at the beginning of the copy of the file and I need the signal is emitted at the end of the copy of file i.e. when the copy of the file is finished.
    How can we detect by programming (Qt or win32) that the copy of a large file (into a directory) is finished?
    is it a Qtbug of QFileSystemWatcher that I must report or should I modify the code of QFileSystemWatcher ?
    I had already watched before the code of QFileSystemWatcher because of the first bug and I don't understand very well this code.
    Could someone help me a little bit ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jan 2017, 21:58 last edited by
      #2

      Hi,

      When copying large files, I usually avoid letting QFile do that alone. I usually do the read/write operation by hand to allow for better feedback and GUI responsiveness.

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

      C 1 Reply Last reply 20 Jan 2017, 22:53
      0
      • S SGaist
        20 Jan 2017, 21:58

        Hi,

        When copying large files, I usually avoid letting QFile do that alone. I usually do the read/write operation by hand to allow for better feedback and GUI responsiveness.

        C Offline
        C Offline
        calebhalvy
        wrote on 20 Jan 2017, 22:53 last edited by
        #3

        @SGaist What do you mean do the read/write operation by hand?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 Jan 2017, 23:01 last edited by
          #4

          Yes, I use QFile for the read and write part.

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

          S 1 Reply Last reply 23 Jan 2017, 08:22
          0
          • S SGaist
            20 Jan 2017, 23:01

            Yes, I use QFile for the read and write part.

            S Offline
            S Offline
            stephane78
            wrote on 23 Jan 2017, 08:22 last edited by stephane78
            #5

            @SGaist It is not copy by programming.
            I have an app made with QFileSystemWatcher(a file synchronization app) that monitors and watches a directory and when I run the app and that I copy a large file into the watched directory, the signal directoryChanged of QFileSystemWtacher is emitted at the beginning of the copy of the large file and it should be at the end of the copy of the large file ...that's my problem

            J 1 Reply Last reply 23 Jan 2017, 08:49
            0
            • S stephane78
              23 Jan 2017, 08:22

              @SGaist It is not copy by programming.
              I have an app made with QFileSystemWatcher(a file synchronization app) that monitors and watches a directory and when I run the app and that I copy a large file into the watched directory, the signal directoryChanged of QFileSystemWtacher is emitted at the beginning of the copy of the large file and it should be at the end of the copy of the large file ...that's my problem

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 23 Jan 2017, 08:49 last edited by
              #6

              @stephane78 said in Copy of large files and QFileSystemWatcher under windows:

              it should be at the end of the copy of the large file

              Why do you think it should? I know, you need it at the end, but maybe someone else needs it at the beginning.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 23 Jan 2017, 09:07 last edited by
                #7

                The only two things I can think of are:

                • try KDirNotify instead
                • Afetr you get the "beginning of copy" signal, store the file size, start a QTimer (500ms?) and check the file size again, if they are equal it probably is finished. I know it's not optimal

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                S 1 Reply Last reply 23 Jan 2017, 09:15
                3
                • V VRonin
                  23 Jan 2017, 09:07

                  The only two things I can think of are:

                  • try KDirNotify instead
                  • Afetr you get the "beginning of copy" signal, store the file size, start a QTimer (500ms?) and check the file size again, if they are equal it probably is finished. I know it's not optimal
                  S Offline
                  S Offline
                  stephane78
                  wrote on 23 Jan 2017, 09:15 last edited by stephane78
                  #8

                  @VRonin Thank you VRonin, but how can I use KDirNotify because I am under windows (the main version of this app must run under windows although there will be perhaps a mac and linux version too) and KDE runs under linux.
                  for the solution with the timer I will perhaps try it...thanks

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 23 Jan 2017, 09:44 last edited by VRonin
                    #9

                    The KDE API has been rewritten in version 5 to be platform independent. The KIO module (which contains KDirNoitify) runs on all desktop platforms (i.e. not on android) by default

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    S 1 Reply Last reply 23 Jan 2017, 09:59
                    3
                    • V VRonin
                      23 Jan 2017, 09:44

                      The KDE API has been rewritten in version 5 to be platform independent. The KIO module (which contains KDirNoitify) runs on all desktop platforms (i.e. not on android) by default

                      S Offline
                      S Offline
                      stephane78
                      wrote on 23 Jan 2017, 09:59 last edited by
                      #10

                      @VRonin thanks I didn't know it. and I should compile the KIO module or library and link it with my app ? I have looked at the url and it seems it uses QtDbus. Can I use QtDbus under windows too? and the signals that are emitted seem to be for files and not for directories ? or is it for directories too ?
                      it seems although more powerful that QfileSystemWatcher, because there are only two signals emitted by QfileSystemWatcher directoryChanged and fileChanged, that's why, I have added code in a class to process all the cases...

                      S 1 Reply Last reply 23 Jan 2017, 11:35
                      0
                      • S stephane78
                        23 Jan 2017, 09:59

                        @VRonin thanks I didn't know it. and I should compile the KIO module or library and link it with my app ? I have looked at the url and it seems it uses QtDbus. Can I use QtDbus under windows too? and the signals that are emitted seem to be for files and not for directories ? or is it for directories too ?
                        it seems although more powerful that QfileSystemWatcher, because there are only two signals emitted by QfileSystemWatcher directoryChanged and fileChanged, that's why, I have added code in a class to process all the cases...

                        S Offline
                        S Offline
                        stephane78
                        wrote on 23 Jan 2017, 11:35 last edited by
                        #11

                        @VRonin Hi it is the class KDirWatch that does that (link text (Ihave asked to the developper of KIO)
                        I will try this class KDirWatch....

                        S 1 Reply Last reply 23 Jan 2017, 14:36
                        0
                        • S stephane78
                          23 Jan 2017, 11:35

                          @VRonin Hi it is the class KDirWatch that does that (link text (Ihave asked to the developper of KIO)
                          I will try this class KDirWatch....

                          S Offline
                          S Offline
                          stephane78
                          wrote on 23 Jan 2017, 14:36 last edited by
                          #12

                          @VRonin, I have watched the code of KDirWatch but I think it doesn't solve my problem, so I will try to use your solution with the timer...;thanks

                          S 1 Reply Last reply 24 Jan 2017, 10:16
                          0
                          • S stephane78
                            23 Jan 2017, 14:36

                            @VRonin, I have watched the code of KDirWatch but I think it doesn't solve my problem, so I will try to use your solution with the timer...;thanks

                            S Offline
                            S Offline
                            stephane78
                            wrote on 24 Jan 2017, 10:16 last edited by
                            #13

                            @VRonin, I have a question before testing, does QFile::size() return the actual size of the large file being copied (so not complete) or does it return 0 ?

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              VRonin
                              wrote on 24 Jan 2017, 10:19 last edited by VRonin
                              #14

                              QFile does not know it's being copied so it will return the actual incomplete file size.

                              One other thing that comes to mind but I did not test is that the copy probably locks the file for writing so attempting QFile::open(QIODevice::WriteOnly) might return false

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              S 1 Reply Last reply 25 Jan 2017, 13:52
                              2
                              • V VRonin
                                24 Jan 2017, 10:19

                                QFile does not know it's being copied so it will return the actual incomplete file size.

                                One other thing that comes to mind but I did not test is that the copy probably locks the file for writing so attempting QFile::open(QIODevice::WriteOnly) might return false

                                S Offline
                                S Offline
                                stephane78
                                wrote on 25 Jan 2017, 13:52 last edited by
                                #15

                                @VRonin OK my problem is solved thanks to you. I have made it with timers.....

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  VRonin
                                  wrote on 25 Jan 2017, 14:36 last edited by
                                  #16

                                  Can you share your final code so other users can benefit from the solution?

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  S 1 Reply Last reply 25 Jan 2017, 15:34
                                  0
                                  • V VRonin
                                    25 Jan 2017, 14:36

                                    Can you share your final code so other users can benefit from the solution?

                                    S Offline
                                    S Offline
                                    stephane78
                                    wrote on 25 Jan 2017, 15:34 last edited by stephane78
                                    #17

                                    @VRonin not all my code because it is a for a Saas software of my company that will be copyrighted (we use Qt-LGPL)...But I can say something important : the condition to test the end of the copy of the large file (in the slot connected to the timeout ) is the equality of previous size and current size and QFile::open(QIODevice::ReadOnly).In my case I use ReadOnly because I use the file and read it after again to use it and it is ok. and it works.a third condition that I have putted in the test is that the current size is not equal 0 because I believe that QFile::size() return 0 so long the file is not finished copied but I am not sure of this condition but if I remove it, I have a visual c++ runtime error that comes .
                                    so you have the three conditions of the test of the end of the copy of the large file and it is the most important.
                                    the left is specific to my software.
                                    but When I detect a new file added thanks to QFileSYstemWatcher I create a new object with the path of the file and in the constructor of this object I create a timer and start it.so for each file added in the directory there is a new timer that will be deleted later after use of this object linked to the file.....
                                    so there are enough hints now to make the things....

                                    S 1 Reply Last reply 25 Jan 2017, 15:38
                                    1
                                    • S stephane78
                                      25 Jan 2017, 15:34

                                      @VRonin not all my code because it is a for a Saas software of my company that will be copyrighted (we use Qt-LGPL)...But I can say something important : the condition to test the end of the copy of the large file (in the slot connected to the timeout ) is the equality of previous size and current size and QFile::open(QIODevice::ReadOnly).In my case I use ReadOnly because I use the file and read it after again to use it and it is ok. and it works.a third condition that I have putted in the test is that the current size is not equal 0 because I believe that QFile::size() return 0 so long the file is not finished copied but I am not sure of this condition but if I remove it, I have a visual c++ runtime error that comes .
                                      so you have the three conditions of the test of the end of the copy of the large file and it is the most important.
                                      the left is specific to my software.
                                      but When I detect a new file added thanks to QFileSYstemWatcher I create a new object with the path of the file and in the constructor of this object I create a timer and start it.so for each file added in the directory there is a new timer that will be deleted later after use of this object linked to the file.....
                                      so there are enough hints now to make the things....

                                      S Offline
                                      S Offline
                                      stephane78
                                      wrote on 25 Jan 2017, 15:38 last edited by
                                      #18

                                      @VRonin, I have tested it with a 1,85 Gb file because we are limited to 2Gb files....

                                      1 Reply Last reply
                                      0
                                      • V Offline
                                        V Offline
                                        VRonin
                                        wrote on 25 Jan 2017, 15:38 last edited by
                                        #19

                                        on the file size =0 I think it's my fault as the docs clearly state:

                                        if the device is closed, the size returned will not reflect the actual size of the device.

                                        so before QFile::open() ,QFile::size() is meaningless

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        S 1 Reply Last reply 25 Jan 2017, 15:44
                                        0
                                        • V VRonin
                                          25 Jan 2017, 15:38

                                          on the file size =0 I think it's my fault as the docs clearly state:

                                          if the device is closed, the size returned will not reflect the actual size of the device.

                                          so before QFile::open() ,QFile::size() is meaningless

                                          S Offline
                                          S Offline
                                          stephane78
                                          wrote on 25 Jan 2017, 15:44 last edited by stephane78
                                          #20

                                          @VRonin so my test is not really correct, I have now read that too in the documentation. because my test is :
                                          if (currentsize&&currentsize==oldsize&&f.open(QIODevice::ReadOnly))

                                          //(and currentsize=f.size();)

                                          S 1 Reply Last reply 25 Jan 2017, 15:47
                                          0

                                          9/23

                                          23 Jan 2017, 09:44

                                          • Login

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