[SOLVED]Read multiple files using QSettings?



  • How can I use QSettings to read multiple ini files within a for loop?

    So basically I read one file, store its contents somewhere and move on to another file until the loop ends, so ill have to use the same QSettings object until the end.


  • Moderators

    You can't do that, you will only damage your data this way. Construct a new QSettings object (put it on a stack within the loop) for every file.
    @
    for (some; conditions; blah) {
    QSettings settings(path);
    // ...
    }
    @



  • Why?

    We use a multiple-configuration store solution, but it doesn't work using QSettings to read multiple files using a loop...

    Instead, we basically have a class that is our central settings interface in the application. That class decides per setting which of the underlying settings files (or registry) it will use for what setting, and who overrules who.



  • I had an unknown number of files that I needed to read and parse. In any case I had to do something like

    for (some; conditions; blah) {
    QSettings* settings = new QSettings(path);
    // ...
    }

    in order to pass the settings object to the function responsible for saving stuff, I hope it is not a stupid move :)

    Thanks.



  • As long as it is clear who will get rid of the QSettings instance once you have passed it to that function...



  • Hey Andre,

    the thing with me though is that I have many files, each one with its' respective settings file, so I decided to read all those files and save them in a list structure. Not sure if its the smartest thing to do :P

    EDIT:
    Yes I do call settings.clear(); on every loop



  • In that case, why not create the instance on the stack in the first place?

    @
    for (some; conditions; blah) {
    QSetting settings(path);
    theProcessingFunction(&setting); // if you must use a pointer, a (const) reference would probably be better
    }
    @

    That's more efficient and less error phrone.



  • True, I don't intend to alter anything on "theProcessingFunction".

    I will do that, thanks :)

    I am though somewhat intrigued by your initial suggestion, if you ever write an article or a tutorial i'd be interested to follow.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.