Skip to content

Instantly share code, notes, and snippets.

@Kernald
Created September 23, 2013 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kernald/6668425 to your computer and use it in GitHub Desktop.
Save Kernald/6668425 to your computer and use it in GitHub Desktop.
Qt settings handler, invokable from QML
#include "settings.hpp"
#include <QtCore/QSettings>
QVariant Settings::getValueFor(const QString &objectName, const QVariant &defaultValue) {
QSettings settings;
// If no value has been saved, return the default value
if (settings.value(objectName).isNull()) {
return defaultValue;
}
// Otherwise, return the value stored in the settings object
return settings.value(objectName);
}
void Settings::saveValueFor(const QString &objectName, const QVariant &inputValue) {
// A new value is saved to the application settings object
QSettings settings;
settings.setValue(objectName, inputValue);
}
#ifndef __SETTINGS_HPP__
#define __SETTINGS_HPP__
#include <QtCore/QObject>
#include <QtCore/QVariant>
class Settings : public QObject {
Q_OBJECT
public:
Q_INVOKABLE static QVariant getValueFor(const QString &objectName, const QVariant &defaultValue);
Q_INVOKABLE static void saveValueFor(const QString &objectName, const QVariant &inputValue);
};
#endif // __SETTINGS_HPP__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment