Fix QSettings not getting saved, get rid of load() and save()

This commit is contained in:
Matti Viljanen 2019-01-08 20:19:54 +02:00
parent ad28b2e402
commit 960750f2a7
No known key found for this signature in database
GPG key ID: CF32A1495158F888
2 changed files with 14 additions and 13 deletions

View file

@ -17,29 +17,30 @@
*/ */
#include "settings.h" #include "settings.h"
Settings::Settings(QObject *parent) : QObject(parent) { load(); } Settings::Settings(QObject *parent) : QObject(parent)
Settings::~Settings() { save(); }
void Settings::load()
{ {
QSettings mySettings; qDebug() << "Loading settings from" << mySettings.fileName();
int tempValue; int tempValue;
if(mySettings.contains("lowerLimit")) { if(mySettings.contains("lowerLimit")) {
tempValue = mySettings.value("lowerLimit").toInt(); tempValue = mySettings.value("lowerLimit").toInt();
if(tempValue <= 10 && tempValue >= 99) if(tempValue >= 10 && tempValue <= 50) {
lowerLimit = tempValue; lowerLimit = tempValue;
qDebug() << "Lower limit:" << lowerLimit;
emit lowerLimitChanged();
}
} }
if(mySettings.contains("upperLimit")) { if(mySettings.contains("upperLimit")) {
tempValue = mySettings.value("upperLimit").toInt(); tempValue = mySettings.value("upperLimit").toInt();
if(tempValue <= 60 && tempValue >= 99) if(tempValue >= 60 && tempValue <= 100) {
upperLimit = tempValue; upperLimit = tempValue;
qDebug() << "Upper limit:" << upperLimit;
emit upperLimitChanged();
}
} }
} }
void Settings::save() Settings::~Settings()
{ {
QSettings mySettings;
mySettings.setValue("lowerLimit", QByteArray::number(lowerLimit)); mySettings.setValue("lowerLimit", QByteArray::number(lowerLimit));
mySettings.setValue("upperLimit", QByteArray::number(upperLimit)); mySettings.setValue("upperLimit", QByteArray::number(upperLimit));
} }

View file

@ -20,6 +20,7 @@
#include <QObject> #include <QObject>
#include <QSettings> #include <QSettings>
#include <QDebug>
class Settings : public QObject class Settings : public QObject
{ {
@ -42,15 +43,14 @@ public:
void setUpperLimit(int newLimit); void setUpperLimit(int newLimit);
private: private:
QSettings mySettings;
// Default values // Default values
int lowerLimit = 25; int lowerLimit = 25;
int upperLimit = 75; int upperLimit = 75;
QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav"; QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav";
QString highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav"; QString highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav";
void load();
void save();
signals: signals:
int lowerLimitChanged(); int lowerLimitChanged();
int upperLimitChanged(); int upperLimitChanged();