Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. INI format without using QSettings

INI format without using QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CentralScrutinizerC Offline
    CentralScrutinizerC Offline
    CentralScrutinizer
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @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.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • CentralScrutinizerC Offline
          CentralScrutinizerC Offline
          CentralScrutinizer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @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.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved