PDA

View Full Version : Settings for a particular file



CCTeam
27th May 2010, 09:31
Hi all,
I need to store a setting related to the file in use; I know that with QSettings I can store the settings for my application, but in my case the setting is to be related to the file, so if I open file A I get the setting SA, if I open the file B I get the setting SB, etc... what is the best practice to do it in QT? Thank you in advance.

Lykurg
27th May 2010, 09:45
Use QSettings and define the file:
QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 );

CCTeam
27th May 2010, 15:16
Does anyone have a small example of using "QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 );" ?
I have some trouble on using it; in particular I don't understand what is the format for "fileName", also because if I use only the fileName (without path) I can't be sure that the name is unique; if I use the path+fileName and after some time the user change directory of the file with Windows explorer then the relative QSetting can't be found... Thank you.

tbscope
27th May 2010, 18:50
All you need to know:
http://doc.qt.nokia.com/4.6/qsettings.html

SixDegrees
27th May 2010, 19:05
Since you've acknowledged that the user may move the file to a different directory, and the filename alone doesn't provide unique identification, take a moment and convince yourself that any amount of fiddling with QSettings will fail to do what you want. Unless you can lock down the file's location in a predefined directory structure which never varies, your only alternative is to store your information in the file itself - either internally (the best solution, if possible) or by embedding information in the filename. The latter is also problematic, since the user can rename the file to whatever they want.

If the files in question are created by you and are unique to your application, store the information internally.

CCTeam
28th May 2010, 07:39
It is exactly what I wanted to do, but I wanted to be sure there were no other better solutions before starting development. Thanks a lot for the help given.