Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: QSettings on Windows with INI files and comments

Tags:

windows

qt

ini

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
like image 907
dwj Avatar asked Nov 11 '09 01:11

dwj


1 Answers

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.

like image 172
György Andrasek Avatar answered Oct 02 '22 05:10

György Andrasek