PDA

View Full Version : How to check a file for changes since last save



nmather
21st April 2007, 22:15
I am making a music app, where the main file format the user can save to is a Song. When the user tries to start or open a new song, I want to check first whether the current song has been changed, and warn the user if it has. My question is, what is the best way to check for changes to a file?

The most obvious way I can think of is have a "changed" flag in Song objects, which gets flagged every time some operation is performed on the Song. Then when you need to check, you just check the flag.

I'm wondering if this is the best way? It seems like it could be cumbersome, if every single action that can change a song will need to remember to set the changed flag when it gets called. The number of actions could be pretty large. It feels like it would be nice if I could abstract it out so actions don't have to worry about the changed flag, they can just perform the action.

I'd love to hear how others have done it in their apps and any general tips :)

wysota
21st April 2007, 22:21
The "proper" way to do it would be to use the undo functionality Qt offers and have two things done at the same time. The down side is that you'd have to implement commands for all things you do in your application.

The "second proper" way would be to use QWidget's "windowModified" property. Of course you'd have to check the flag yourself everytime you change the document.

If the object is not a widget, then you have to do it the way you suggested.

nmather
21st April 2007, 23:43
Thanks, I didn't know about the Qt Undo framework! That would be ideal.

Unfortunately the song file object isn't a widget -- it needs to be separated from Qt as it has to work with other applications, using other toolkits.

However, I wonder if I could write a Qt wrapper for this object that could then utilize the undo framework...