[Interest] Write QSettings to a QString in INI format

Murphy, Sean smurphy at walbro.com
Thu Oct 24 17:53:08 CEST 2019

I'd like to be able to have QSettings write out my settings to an INI file format, but I'd like to avoid writing it to an actual file, instead just writing it to "something" in memory (for example, a QString, QByteArray, QBuffer, etc.). 

As far as I can tell, there doesn't seem to be any way to do that:
- QSettings doesn't have a constructor that takes any sort pointer to a memory type object. It only has constructors for dealing with the platform specific settings locations (registry on Windows, plists on Mac, etc), or it has the one constructor that takes a filename and will write to a file.
- It appears (unless I'm reading it wrong) that the QSettings::registerFormat allows you to customize how settings are written out, but it can only be used in conjuction with the filename constructor and will always open a file and write to it, but it allows you to customize the format of how the key/value pairs are written out - there is an example that writes it out in XML. This seems like the opposite of what I want to do, I want QSettings to write out the exact same format as it already does with the QSettings::IniFormat, I just want to keep it all in memory and avoid creating a file.

So I if all the above is correct, and there is no way to write an INI formatted string to memory, and given the fact that I really don't want a file in the first place, I decided to look at writing the settings out to a QTemporaryFile, then just reading that data back in as a string, and then let the QTemporaryFile go out of scope and clean itself up. But according to the following test code, QSettings does not seem to play nicely with QTemporaryFile:

void MainWindow::writeSettings()
// comment/uncomment the *define* below to switch implementations

    // using a QTemporaryFile doesn't seem to work
    QTemporaryFile tempFile;
    bool ok = tempFile.open();
    // if instead you use a regular QFile, it works
    QFile tempFile("tempFile.ini");
    bool ok = tempFile.open(QIODevice::ReadWrite);

        qDebug() << "Opened" << tempFile.fileName();
    } else {
        qDebug() << "Unable to open" << tempFile.fileName();

    QSettings settings(tempFile.fileName(), QSettings::IniFormat);
    settings.setValue("string", "hello");
    settings.setValue("int", 2);

    // using QTemporaryFile always produces an AccessError here, 
    // QFile reports NoError
    qDebug() << settings.status();

    QFile file(tempFile.fileName());
    ok = file.open(QIODevice::ReadOnly);
    QString settingsString = file.readAll();
    // at this point, settingsString should contain
    //      [General]
    //      string=hello
    //      int=2
    // it does if I use QFile, but is empty when using a QTemporaryFile
    qDebug() << settingsString;

So I guess I can use QFile to create my own file, do what I need to do, then remove it myself when I'm done, but is there any alternative way to get what I want - QSetting written to a QString?


This message has been scanned for malware by Forcepoint. www.forcepoint.com

More information about the Interest mailing list