Important: Please read the Qt Code of Conduct -

QSettings ini format special chars in key

  • Has anyone had experience with QSettings and specifically line comments?

    I cannot find decent documentation. I've tried setting encoding, nothing stops this double encoding of keys.

    QSettings settings(QSettings::IniFormat, QSettings::Scope::SystemScope, QCoreApplication::instance()->organizationName(),  QCoreApplication::instance()->applicationName());
    // settings.setIniCodec("UTF-8");
            static QString value = QString("# ini file comment");
            settings.setValue("# ## commentedKey",value);
            settings.setValue("; another commentedKey",value);

    Results in:

    %23commentedKey=# ini file comment
    %23%20%23%23%20commentedKey=# ini file comment
    %3B%20another%20commentedKey=# ini file comment

    To add insult to injury, if I create a file with either # or ; line comments, it reads fine but if even if I do a group only write - the whole file gets junked. This is insane.

  • It also encodes non space whitespace like tabs to \t even for value data. There's no documentation on this behavior and the only parts I find that might be relevant :

    Section and Key Syntax
    Setting keys can contain any Unicode characters. The Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on macOS and iOS uses case-sensitive keys. To avoid portability problems, follow these simple rules:

    seem to indicate it can even handle unicode, wheras if I try utf-8 - it still just junks ini files.

  • @6thC
    Are you asking whether QSettings allows for comment lines starting with # or ;? If so, I believe we have been through this before, and it does not.

  • Moderators

    QSettings uses ini-like files internally. It does not follow INI specification and if it does work with external INI files/readers, it is a coincidence. As annoying as this answer is, it is also the truth. If you need a "real" INI reader/writer, you need to use some third party library for that.

  • Thanks, it's been a couple years now since I've looked at the configuration side of things, well, not entirely true, adding plenty of read groups etc, but they've all been just reads.

    @JonB you mean me specifically or it's been discussed before, I don't remember but me forgetting something wouldn't be the first time that's happened. I did search quite a bit so got the feeling this was the case.

    @sierdzio Yup, that's how I've experienced it too, I have only now begun making writes back into my configuration file and noticed reads are just fine as you say, which is actually the annoying bit, we went for two years thinking we could write a file back similar to what we read, using the framework.

    For now I've dropped the comments and just had an "example file". This is the first time since establishing the configurations that we've begun to add more AND write values back, up to now it's been strictly reads with no GUI feedback.

    Also happens that my INI file had quite a lot of comments, examples of valid options, explanations, it was well documented and in one write turned the lot into an ugly mess. Previously commented out configurations now awoke and would have be a duplicate key - but were with junked leading data ( %23 and %20 's) . It was just a totally broken affair.

    Changing parts that never changed was also quite the rude surprise - it's like the whole file gets re-created even for a group only line / setting write! But it doesn't just keep the unchanged areas 1:1 - it still does it's double encoding for the entire file.

    It was just a startling and rude surprise - I can deal with it. Thanks guys.

  • @6thC said in QSettings ini format special chars in key:

    @JonB you mean me specifically or it's been discussed before, I don't remember but me forgetting something wouldn't be the first time that's happened. I did search quite a bit so got the feeling this was the case.

    Gosh, no! I meant the "Royal We" :) I recall answering about this in another thread a while ago (e.g. and

    As @sierdzio said, the .ini file support from QSettings is only for its own puprposes. All it really guarantees is that you can read back a file that it has written. You may be able to read an external .ini file, but if you write it back it can change to non-.ini format. Remember Qt is platform-independent, and the files you are talking about are really Windows-only.

Log in to reply