Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

INI format without using QSettings



  • Can Qt read/write INI formatted files without using the QSettings class? This is a fine class but from what I understand but (please correct me if I'm wrong) you don't have control over when the INI file is written am I right? This is for global application settings, so it doesn't really fit my use case....

    I want to have a bunch of key/value type settings that exist in an object stored to an arbitrary INI file when the user clicks "Save Job", not whenever QSettings feels like it. I know you can force it to commit changes to the settings file using the sync() method, but that's not really what I'm after. I just want the formatting and parsing capability. Is that a thing? I mean I could make my own custom INI parser (or something else like JSON or XML), but i'm wondering if this already exists in Qt


  • Lifetime Qt Champion

    Hi,

    If you are thinking of a secondary class that does the same job as QSettings for ini files, then no, nothing in the public API. AFAIK QSettings doesn't sync as it see fit. It uses what the platform provides which might do things a bit differently for its native format. However of the INI file, I don't think you would have any trouble regarding that.



  • @CentralScrutinizer I see your concern about QSettings writing to the destination file as a you setValue(key, value) statements, without your control except just the sync() action to flush
    What I'd do in that scenario is create a QMap and keep inserting the (key,value) pairs at will, and only set the values to the QSettings object when the user wants to save the settings. Pseudo-code:

    QMap<QString, QVariant> mapSettings;
    ...
    mapSettings["key1"] = "value1";
    mapSettings["keyn"] = valueN;
    ....
    // user want to save
    for (auto key : mapSettings.keys()) {
        settings.setValue(key, mapSettings.value(key);
    }
    settings.sync()
    

    I'd prefer to stay with QSettings as it provides multi-platform support and you'll avoid writing new for what there's already support.



  • Thanks for the suggestions. It's actually super simple, you just have to pass the file path you want in to the constructor of the QSettings object. It works in Windows and Linux.



  • @CentralScrutinizer yes, but the settings will be written as soon (or differed to improve performance) as you use the setValue() method for any key/value (or forcing writes with sync()) but this is something I understood you want to avoid

    I want to have a bunch of key/value type settings that exist in an object stored to an arbitrary INI file when the user clicks "Save Job", not whenever QSettings feels like it

    Anyway, if it's solved it's good.


Log in to reply