Results 1 to 7 of 7

Thread: Check if a file has been modified

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Check if a file has been modified

    Hi, I'm creating an image viewer. It allows the user to modify any image by opening a simple image editor (created using Qt, too). Everytime that the editor is closed, the image viewer has to check if the current image that the user is viewing has been modifed and update it in this case. My question is what's the best way to know when an image has been modified? I've thought in two solutions: 1) emiting a signal from the image editor everytime that an image file has been modified with it's name. 2) Check, when the image editor is closed, if the date that QFIleInfo::lastModified returns from the current image file if it's greater than the date when the editor was opened, for example. With the second method the image viewer only has to check one time if the file has been modified, but I'm not sure if calling QFileInfo::lastModified() is portable through all platfforms (I have a suspicious that it doesn't works with win98...). Which method do you think that it's better?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check if a file has been modified

    If the image editor is a separate application, you can do one of two things:
    1. return a value from the editor stating that the file was modified. You can read the value with QProcess:exitCode().
    2. when the editor returns, check if the file was modified (by comparing times of last modification and/or file size).

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

    Dark_Tower (13th April 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check if a file has been modified

    Quote Originally Posted by wysota
    2. when the editor returns, check if the file was modified (by comparing times of last modification and/or file size).
    Do you think that all the file systems save the last modification date of the files? I am still a bit uncertain of using this method... (although I think that's the best solution )

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check if a file has been modified

    There is a QFileInfo::lastModified() and its docs don't state that this is not portable, so I guess they do. But option 1. is fine too. And you don't rely on anything. On the other hand some other application might have modified the file in the meantime. With option 1. you won't notice that.

  6. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check if a file has been modified

    Thanks again wysota.

    Quote Originally Posted by wysota
    There is a QFileInfo::lastModified() and its docs don't state that this is not portable, so I guess they do. But option 1. is fine too. And you don't rely on anything.
    I haven't comented the first option because the editor is not opened form a different process (maybe in the future... ) Also, I activate the flag destructiveOnClose for the editor.

    Quote Originally Posted by wysota
    On the other hand some other application might have modified the file in the meantime. With option 1. you won't notice that.
    You're right, I haven't noticed that... So maybe the solution of emiting a signal is the best in this case. What do you think? or maybe there's still another solution?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check if a file has been modified

    It depends what exactly you want to do.

    Probably the best would be just to have a modifed() flag in your editor and to read its state upon exit. For example it could work just like QDialog::exec() -- returning a value (for example true for modified and false for not modified). Of course the editor would be responsible for setting that flag.

  8. #7
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check if a file has been modified

    Thanks wysota. Finally what I will do is use the signal that the editor has to emit to let know the viewer when it closes to pass a booolean that informs if the file has been modified (I think it's a good solution, too)
    Last edited by Dark_Tower; 13th April 2006 at 21:30.

Similar Threads

  1. Check for invalid file name in QFileDialog
    By darren in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2008, 14:43
  2. Check file is modified?
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 6
    Last Post: 20th September 2007, 13:03
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43
  5. Retrieving modified date of file using QHttp
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 3rd December 2006, 23:20

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.