views:

291

answers:

4

I'm writing a Qt application and will need to store the settings for the program. I want them to be easily editable by non-advanced users yet be flexible enough for advanced users (thus allow easy automated editing via other programs, scripts, whatever). QSettings does provide two formats, the native format, which for Windows is registry, and the INI format, which is native for most other platforms. INI is fine, but seeing @QString(...) or other Qt stuff in there isn't really readable and is kinda error-prone.

The registry isn't great either. It wasn't designed to be messed with and thus not exactly good for editing or advanced usage; it does solve the problem of synchronization across threads and multiple QSettings objects (so I don't wipe everything out, though I can just use one global object, protected by a read-write locker). I'm looking at XML, but it is pretty darned verbose and it does require writing a QSettings format (not really an issue) but very flexible.

I know other alternatives to XML exist but I'm not really familiar with them; I certainly don't want to write a parser, exception for my own final format, not for the base thing.

Update - Note: I will not bypass QSettings at all, I will just write a format for it - which looks like it is just two function pointers (for a read and for a write function) passed to a static function and then I can use my format.

Update 2: I am also worried about Linux servers, which usually don't have a GUI.. I want people to be able to edit the configuration easily from the server via nano or something similar, without using the manager (yes, I will have a daemon server and a remote GUI manager).

A: 

Another way to look at verbosity is content robustness. If for any reason (an alpha particle bombarding the disk, causing single bit to flip), you can still go in there and make the necessary error correction... so there is benefit, but also cost associated with it ... with a binary file, a single bit error means the data is completely lost

vtd-xml-author
That's true, but in this case probably beside the point. I would definitely worry about silent data corruption and manual recovery for documents, work items and the like, but that's probably not a big issue for settings storage.
Mihai Limbășan
Not much of an issue for settings, just the user manually editing the file and breaking the structure, but that can be checked and even fixed (or plain removed/ignored) easily.
iconiK
+1  A: 

I would start with doing the QSettings via .ini files, and see if you end up with many problems with it. If there aren't problems, then there's no need to go for an XML solution. If you do end up wanting the XML solution, you could (as mentioned) add a formatter for the QSettings objects.

Caleb Huitt - cjhuitt
+4  A: 

You can use the QSettings class to achieve this. It's an abstraction class that allow your applications to store its settings in order to retrieve them at next launch.

Save settings:

QSettings settings("ValueName",  "Value");

Read settings:

QString v = settings.value("ValueName");
Patrice Bernassola
Please read my question again. I do know that, and will use QSettings, but I am worried about the underlying format. QSettings provides an INI format and a Windows registry format.
iconiK
QSettings is using the "standard" storage format regarding the OS. That's why I spoke about abstraction. This class allow you to use the standard format of each system even if they are not the same. This the way Qt saves values for your app. No need to do it by yourself unless you want to save complex object. In this case I think XML is the better way.
Patrice Bernassola
The OP is saying he wants the users of his app to edit the settings with ease, not that he wants to save settings with ease (because he already is)
blwy10
+1  A: 

If for whatever reason you end up bypassing QSettings and considering XML for your configuration file, I suggest you go look at JSON or YAST, depending on how you like the available libs.

As a sidenote, if you don't intend to have users ever edit the file manually, just choose whatever is easiest for you (QSettings?) and move on with your life, since the choice of format will not matter a single bit (har har).

Tiberiu Ana
I will look at them and will see how to write a QSettings format if I choose eiher.It will matter when moving across computers. A file is as easy as copying the file. A registry key needs to be exported to a file then imported. And regedit might complain if you give it a .reg file created with a newer regedit.
iconiK