Results 1 to 3 of 3

Thread: QFileSystemWatcher best way to detect change

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QFileSystemWatcher best way to detect change

    Hello.
    This library allows you to detect whether a folder or a file has changed.
    Specifically, if you watch a file, whatever the change, it will send the signal that the file has changed, but you don't know whether only the content has changed or the file was deleted.
    If you watch a folder, it will say you that something has changed only if a file has changed (removed/deleted/renamed) on the top folder you've set (not inside its subfolders), and again, you can't even know what kind of change has been made inside the folder.

    What I want to do is to add all the files of the given folder at startup of the program and add/remove any file that has been added/removed from the given folder to a list (for example to a list_widget)
    What I've done to the program so far is to add all the files recursively to a list (let's say a QStringList) on startup of a selected folder and then track every single file for changes and also track and all the subfolders of the given one and itself for changes. It works.

    The main problem is that it turns sometimes to be very slow if there are too many changes in too many files so as to add to the list every file that is added to the given folder or its subfolders and remove any file from the list if it is removed from the given folder or its subfolders as well.
    Deletion is quite easy, because I get the signal that the file changed, I detect whether it exists using the QFile library and if it doesn't it has simply changed and I don't remove it from the list.
    Addition is challenging. First of all, I have to detect whether a whole folder has been added or simply (a) file(s). So I list all the subfolders of the given (main) folder and I compare it with the list of all the tracked folders (one-by-one). If there's a folder that doesn't exist on the tracked ones I add it. Then, if there are not differences, I get the files of the folder to a list and I compare it to the list containing all the tracked files. Again, if there's a file that's not present to the list with all the tracked files, its being added.

    Well, this is quite complicated but I cannot think of a better way considering that qt don't tell me what kind of change happened automatically.

    So, I'd like to see if there's any other better way to do such a thing. I hope I was understandable :P

    Thanks in advance for any response!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher best way to detect change

    Well, it depends on the number of files beeing watched, but if there is not too much, I'd choose too watch the directories only, and whenever a change occures I reload the entire directory contents.
    To avoid too much work when multiple files are added/removed at the same time, I would link directory-changed-signals to a timer. Thus, any change (re)start the timer and I reload the directory only after a certain interval is elapsed.

  3. The following user says thank you to totem for this useful post:

    hakermania (12th October 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFileSystemWatcher best way to detect change

    Yeah, the problem is that the files can be up to 1000, I can never know what the user may do, he'll maybe add 10.000 files at once :P
    I thought of having a timer resetting each time a folder's contents have changed. The timer, if running will restart to 1 second for example. When the timer finally timeouts (=means that there hasn't been any change inside the folder for one second) I will recursively list again the contents of the folder and re-add them to the list... This also includes the process of removing the files of the folder and re-add them so as to remove duplicate entries but it's maybe quicker rather than comparing huge string lists between them.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

Similar Threads

  1. detect system time change
    By ahmdsd_ostora in forum Qt Programming
    Replies: 5
    Last Post: 29th October 2019, 12:04
  2. Replies: 4
    Last Post: 3rd August 2010, 07:17
  3. QFIleSystemWatcher
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 08:21
  4. QFileSystemWatcher
    By Kumosan in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2008, 20:52
  5. QFileSystemWatcher
    By minty in forum Qt Programming
    Replies: 13
    Last Post: 22nd May 2007, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.