PDA

View Full Version : Check if a file has been modified



Dark_Tower
13th April 2006, 10:36
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?

wysota
13th April 2006, 16:42
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).

Dark_Tower
13th April 2006, 18:22
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 ;) )

wysota
13th April 2006, 19:47
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.

Dark_Tower
13th April 2006, 20:56
Thanks again 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.



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?

wysota
13th April 2006, 21:01
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.

Dark_Tower
13th April 2006, 21:27
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) :)