Der folgende Beispielcode zeigt die Verwendung von QSettings zur Speicherung von Konfigurationsdaten wie Fenstergeometrie, Fonts, Farben,etc.

Für einige Daten hat QSettings schon passende Methoden, für andere hat der entsprechende Datentyp Methoden zur Konvertierung von/nach QString (siehe Farbe und Font) und für die restlichen kann man in seiner eigenen Configklasse leicht Hilfmethoden implementieren (siehe Rechteck)

Die QSettings Instanz wird hier nur für den Zugriff verwendet, also Lesen und Schreiben. Das Schreiben findet nämlich nur im Destruktor von QSettings statt und so ermöglicht man sich, jederzeit speichern zu können.

Der String, der an openGroup in initSettings() übergeben wird, ist zugleich die Basis für den Dateinamen der Settingsdatei in ~/.qt
HIer wird die Datei ~/.qt/applicationconfigexamplerc erzeugt.

Code:
 
#include <qapplication.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qsettings.h>
#include <qvbox.h>

class ApplicationConfig
{
public:
    ApplicationConfig() : geometry(100, 100, 300, 200),
                          bgColor(255, 0, 0),
                          toggleState(false),
                          m_settings(0)
    {
        reload();
    }

    void reload() {
        initSettings();
        geometry = readEntry("/geometry", geometry);

        QColor color(m_settings->readEntry("/label/bgcolor", bgColor.name()));
        if (color.isValid()) bgColor = color;

        QFont f;
        if (f.fromString(m_settings->readEntry("/label/font", font.toString())))
            font = f;

        bool ok;
        bool b = m_settings->readBoolEntry("/button/togglestate", toggleState, &ok);
        if (ok) toggleState = b;

        delete m_settings;
        m_settings = 0;
    }

    void write() {
        initSettings();
        writeEntry("/geometry", geometry);

        m_settings->writeEntry("/label/bgcolor", bgColor.name());

        m_settings->writeEntry("/label/font", font.toString());

        m_settings->writeEntry("/button/togglestate", (toggleState ? "true" : "false"));

        // deleting does the actual writing
        delete m_settings;
        m_settings = 0;
    }

public:
    // application config values
    QRect  geometry;
    QColor bgColor;
    QFont  font;
    bool   toggleState;

private:
    inline void initSettings() {
        m_settings = new QSettings();
        m_settings->setPath("mrunix.de", "ApplicationConfigExample");

        m_settings->beginGroup("ApplicationConfigExample");
    }

    inline QRect readEntry(const QString& name, const QRect& def) {
        QString entry = m_settings->readEntry(name);
        if (entry.isEmpty()) return def;

        QStringList parts = QStringList::split(",", entry);
        if (parts.count() != 4) return def;

        int values[4];
        uint index = 0;
        for (QStringList::iterator it = parts.begin(); it != parts.end(); ++it, ++index)
        {
            bool ok;
            values[index] = (*it).toInt(&ok);
            if (!ok) return def;
        }

        return QRect(values[0], values[1], values[2], values[3]);
    }

    inline void writeEntry(const QString& name, const QRect& rect) {
        QString entry;
        entry.sprintf("%d, %d, %d, %d", rect.x(), rect.y(), rect.width(), rect.height());
        m_settings->writeEntry(name, entry);
    }


private:
    QSettings* m_settings;
};


int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    ApplicationConfig config;

    QVBox box(0);
    app.setMainWidget(&box);
    QPushButton* button = new QPushButton("Toggle me", &box);
    button->setToggleButton(true);
    QLabel* label = new QLabel("a quick brown fox jumps over a lazy dog", &box);

    // apply config values
    box.setGeometry(config.geometry);
    button->setOn(config.toggleState);
    label->setFont(config.font);
    label->setPaletteBackgroundColor(config.bgColor);

    box.show();
    int rc = app.exec();

    // save new config values
    config.geometry = box.geometry();
    config.toggleState = button->isOn();

    config.write();

    return rc;
}
Ciao,
_