Qt-interest Archive, December 2007
Creating QSettings manually ?
Message 1 in thread
I want to use QSettings to store app. specific data but
there are a couple of things I'm unclear about:
1. Is there a QSettings editor available that can be
used manually to create app. specific settings, when
an application is first installed ? I'm aware of the
QSettings demo, but I'm not sure if that can be
redistributed, or what that would entail ? (Dumping
the exe into the release ?)
2. Is there a specific test for an "empty" settings
object ? I'm considering the case when an app is
run, and creates a QSettings object before the
settings have been stored, or if the settings have
been deleted since they were first created. I guess
it's possible to look at the number of keys, or for
a specific key, but I would prefer something that
told me explicitly that QSettings could find no
appropriate settings.
--
[ signature omitted ]
Message 2 in thread
> 1. Is there a QSettings editor available that can be used
> manually to create app. specific settings, when an
> application is first installed ? I'm aware of the QSettings
> demo, but I'm not sure if that can be redistributed, or what
> that would entail ? (Dumping the exe into the release ?)
No idea on this one. I don't typically ship a settings file with what I
send out. See below for my general approach.
> 2. Is there a specific test for an "empty" settings object ?
> I'm considering the case when an app is run, and creates a
> QSettings object before the settings have been stored, or if
> the settings have been deleted since they were first created.
> I guess it's possible to look at the number of keys, or for a
> specific key, but I would prefer something that told me
> explicitly that QSettings could find no appropriate settings.
What I do is make sure I've got reasonable defaults in all my value()
calls. So for me, the first time the application is run, there is no
settings file, so for each value() call, there is no existing key, so
the default value is used. Then when my application is quit the first
time, all the settings get written out to the settings file. Next time
the application is run, all the keys are found so the values from the
settings file are used. If you need to know whether this is the first
time your application has been run, you can do something like this in
your constructor:
QSettings settings;
int first_run = settings.value("first_run", 0).toInt();
// at this point first_run contains the value associated with the key
// "first_run" or 0 if the key "first_run" wasn't found in the settings
file
if (first_run == 0)
{
// do whatever you need for the first run
}
And then in your destructor do:
settings.setValue("first_run", 1);
So the only time first_run will equal 0 is if no settings file exists
(or I guess if someone messed with the settings file by hand), all
subsequent launches will return a value of 1 for first_run, and then you
can be fairly confident that the rest of the settings will be as sane as
you wrote them out.
Sean
--
[ signature omitted ]
Message 3 in thread
On tirsdag den 11. December 2007, Murphy, Sean M. wrote:
> What I do is make sure I've got reasonable defaults in all my value()
> calls.
And this is the only way it should be done. This way your application will
still work even if the user does something bad, like destroying the registry
settings or deleting the ini/rc file.
Offering a prebuilt ini file only makes one thing easier: To make it easy to
see what settings are used by default in your application. Finding these in
the code can be an issue. But I would still vote for not pushing this to a
user issue.
Bo.
--
[ signature omitted ]
Message 4 in thread
On 11.12.07 18:48:01, Stephen Collyer wrote:
> I want to use QSettings to store app. specific data but
> there are a couple of things I'm unclear about:
>
> 1. Is there a QSettings editor available that can be
> used manually to create app. specific settings, when
> an application is first installed ?
You can use any text editor to create a file that can be used with
QSettings.
> 2. Is there a specific test for an "empty" settings
> object ? I'm considering the case when an app is
> run, and creates a QSettings object before the
> settings have been stored, or if the settings have
> been deleted since they were first created. I guess
> it's possible to look at the number of keys, or for
> a specific key, but I would prefer something that
> told me explicitly that QSettings could find no
> appropriate settings.
I don't quite understand what you want here, do you want a
QSettings::isFileEmpty() or some such? If so a simple QFileInfo::size()
could help here. Else childKeys and childGroups is what you should
check I guess.
Andreas
--
[ signature omitted ]
Message 5 in thread
QSettings are stored in the registry on windows, so be aware that referring to a file doesn't work on all platforms.
Cheers,
Peter
> -----Ursprüngliche Nachricht-----
> Von: Andreas Pakulat [mailto:apaku@xxxxxx]
> Gesendet: Dienstag, 11. Dezember 2007 20:33
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: Creating QSettings manually ?
>
> On 11.12.07 18:48:01, Stephen Collyer wrote:
> > I want to use QSettings to store app. specific data but
> > there are a couple of things I'm unclear about:
> >
> > 1. Is there a QSettings editor available that can be
> > used manually to create app. specific settings, when
> > an application is first installed ?
>
> You can use any text editor to create a file that can be used with
> QSettings.
>
> > 2. Is there a specific test for an "empty" settings
> > object ? I'm considering the case when an app is
> > run, and creates a QSettings object before the
> > settings have been stored, or if the settings have
> > been deleted since they were first created. I guess
> > it's possible to look at the number of keys, or for
> > a specific key, but I would prefer something that
> > told me explicitly that QSettings could find no
> > appropriate settings.
>
> I don't quite understand what you want here, do you want a
> QSettings::isFileEmpty() or some such? If so a simple
> QFileInfo::size()
> could help here. Else childKeys and childGroups is what you should
> check I guess.
>
> Andreas
>
> --
> You would if you could but you can't so you won't.
>
> --
> To unsubscribe - send a mail to
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 6 in thread
On 12.12.07 09:37:42, Peter Prade wrote:
> QSettings are stored in the registry on windows, so be aware that referring to a file doesn't work on all platforms.
Only if you use the native format. Just make sure you use the IniFormat
and you can be sure your settings are cross-platform.
Andreas
--
[ signature omitted ]
Message 7 in thread
I think you can solve both problems quite easily:
1. Use the QSettings::Iniformat because then all settings are saved in a
normal text file, which can be edited by any text editor.
2. What about the function "QSettings::allKeys()"? If it returns an empty
QStringList, then no settings were saved before.
I hope that will help you,
Kai
On Dec 11, 2007 7:48 PM, Stephen Collyer <scollyer@xxxxxxxxxxxxxxxx> wrote:
> I want to use QSettings to store app. specific data but
> there are a couple of things I'm unclear about:
>
> 1. Is there a QSettings editor available that can be
> used manually to create app. specific settings, when
> an application is first installed ? I'm aware of the
> QSettings demo, but I'm not sure if that can be
> redistributed, or what that would entail ? (Dumping
> the exe into the release ?)
>
> 2. Is there a specific test for an "empty" settings
> object ? I'm considering the case when an app is
> run, and creates a QSettings object before the
> settings have been stored, or if the settings have
> been deleted since they were first created. I guess
> it's possible to look at the number of keys, or for
> a specific key, but I would prefer something that
> told me explicitly that QSettings could find no
> appropriate settings.
>
> --
> Regards
>
> Steve Collyer
> Netspinner Ltd
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>