Home > Articles, Libraries & Frameworks > Portable and Abstract version for storing settings in QT applications.

Portable and Abstract version for storing settings in QT applications.

The QT provides an adorable way to store the settings of a program with a GUI (and not only) by means of the QSettings library. Below, we quote a personal version of the project ‘parkman‘ (Parking Manager).

In this application, two methods were used (one for the safe reading of settings and one for storing settings). I mention “safe reading” because the reading method also performs control over the data and the configuration settings file (if there is any problem, it then creates a configuration file with default values).

Methods for reading and writing program settings:

...
/* write the settings of the program. */
void
SettingsForm::writeSettings() {
    QSettings s(setsAppOrg, setsAppName);

    s.beginGroup("parking");
    s.setValue("capacity", parkingCapacitySpin->value());
    s.endGroup();

    s.beginGroup("payment");
    s.setValue("timeslice", chargeTimesliceSpin->value());
    s.setValue("charge_precision", chargePrecisionSpin->value());
    s.endGroup();

    /* try to write down the settings. */
    s.sync();

    /* if the app cannot write the settings. */
    if (s.status() != QSettings::NoError) {
        /* show a message. */
        QMessageBox::critical(this, infoMsgTitleStr, cantWriteSetsStr);

        /* terminate the program. */
        exit(EXIT_FAILURE);
    }
}

/* establish the settings of the program.
   also check, create them if not exist. */
void
SettingsForm::establishSettings() {
    QSettings s(setsAppOrg, setsAppName);

    s.beginGroup("parking");
    if (s.value("capacity").isNull()
        || s.value("capacity").toInt() < MIN_PARKING_CAPACITY
        || s.value("capacity").toInt() > MAX_PARKING_CAPACITY) {
        s.setValue("capacity", DEF_PARKING_CAPACITY);
    } else {
        sets.parkingCapacity = s.value("capacity").toInt();
    }
    s.endGroup();

    s.beginGroup("payment");
    if (s.value("timeslice").isNull()
        || s.value("timeslice").toInt() < MIN_TIMESLICE
        || s.value("timeslice").toInt() > MAX_TIMESLICE) {
        s.setValue("timeslice", DEF_TIMESLICE);
    } else {
        sets.timeslice = s.value("timeslice").toInt();
    }

    if (s.value("charge_precision").isNull()
        || s.value("charge_precision").toInt() < MIN_CHARGE_PRECISION
        || s.value("charge_precision").toInt() > MAX_CHARGE_PRECISION) {
        s.setValue("charge_precision", DEF_CHARGE_PRECISION);
    } else {
        sets.chargePrecision = s.value("charge_precision").toInt();
    }
    s.endGroup();

    /* try to write down the settings. */
    s.sync();

    /* if the app cannot write the settings. */
    if (s.status() != QSettings::NoError) {
        /* show a message. */
        QMessageBox::critical(this, infoMsgTitleStr, cantWriteSetsStr);

        /* terminate the program. */
        exit(EXIT_FAILURE);
    }
}
...
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: