-
Notifications
You must be signed in to change notification settings - Fork 34
/
settings.cpp
38 lines (33 loc) · 1.35 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "settings.h"
#include <QCoreApplication>
#include <QStringList>
#include <QDebug>
Settings::Settings(const QString &organization, const QString &application, QObject *parent) :
QSettings(organization, application, parent)
{
// We pre-parse these incase the app makes heavy use of some settings...
// Parse the command line, storing the values in parsedArguments
QString key = QString::null;
for(QString arg : QCoreApplication::arguments()) {
// Is this a new parameter?
if(arg.startsWith("--")) {
// Extract the key, and pre-emptively set as a flag
key = arg.mid(2);
parsedArguments.insert(key,"true");
} else {
if(!key.isNull()) {
// Store value using the key
parsedArguments.insert(key,arg);
key = QString::null;
}
}
}
// Show what happened
for(QString arg : parsedArguments.keys()) {
qDebug() << "detected command line argument" << arg << "with value" << parsedArguments.value(arg);
}
}
QVariant Settings::valueFromSettingsOrCommandLine(const QString &key, const QVariant &defaultValue) {
// Is this a setting which is being overided by the command line arguments?
return parsedArguments.contains(key) ? parsedArguments.value(key) : QSettings::value(key,defaultValue);
}