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);
}
}
...


