tags:

views:

1322

answers:

1

I have an application I'm writing using Qt 4.5.2 on Windows. I'm storing some settings in an INI file and using QSettings to load and save the settings. I'd like to have some comments in the INI file.

For example:

; Meta-info to store with the file
[General]
MainWindow\size=@Size(1280 600)
MainWindow\pos=@Point(0 300)
Debugging=true

However, I've found when I load the settings file with

QSettings settings( "settings.ini", QSettings::IniFormat );

the comments are stripped out of the file. The INI file is re-written after loading by a call to QSettings::sync() (this is done automatically by the constructor). Is there a way to preserve the comments after syncing?

Preemptive comments:

  • I want INI files in Windows for future cross-platform compatibility
  • I want to store meta-info in the file for reference outside of the application
  • I am considering making the meta-info a section of the INI and using the name=value rules but would prefer to keep the information as a comment
+1  A: 

QSettings has no concept of "save". All the changes you do to it is considered to be final, and written to disk often and transparently.

In the documentation of QSettings, there is no mention about comments in ini files. It does makes some sense: after all, it can be a registry value, too. Treat it like a generated file: it is one.

Here's my suggestion:

[General]
Comment = @String(Meta-info to store with the file)
MainWindow\size=@Size(1280 600)
MainWindow\pos=@Point(0 300)
Debugging=true

I don't know if it works, play around with it to see how it actually stores the string. Oh, and make sure you either set it from code or document it properly, to avoid accidentally using the same identifier from within the program.

Jurily
Key line: "Treat it like a generated file: it is one." I think that says it all. If I want to do non-Qt settings stuff with the file then it's not really a Qt settings file.
dwj