How to create a class based on QSettings
-
I'm trying to create a class based on QSettings, so I can create a constructor with default values, for example:
#ifndef MYSETTINGS_H #define MYSETTINGS_H #include <QSettings> class MySettings : public QSettings { Q_OBJECT public: MySettings(QString fileName = "", QSettings::Format format = QSettings::IniFormat, QObject *parent = 0); signals: public slots: }; #endif // MYSETTINGS_H
CPP:
#include "mysettings.h" #include <QStandardPaths> MySettings::MySettings(QString fileName, QSettings::Format format, QObject *parent) { if( fileName.isEmpty() ) { QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); fileName = path + "/Settings.ini"; } }
So I can use it as follows:
MySettings settings(); settings.beginGroup("Test"); settings.setValue("First Value", 1); settings.endGroup();
Instead of:
QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); QSettings settings2(path + "/Settings2.ini", QSettings::IniFormat); settings2.beginGroup("Test2"); settings2.setValue("Second Value", 2); settings2.endGroup();
Is it possible?
Thanks
-
Hi
yes that should be possible.Anyway, docs says
" Constructing and destroying a QSettings object is very fast."So you could also just use a function to return a QSettings and in that way hide the
StandardPaths::writableLocation(QStandardPaths::ConfigLocation); etc -
@mrjj thanks.
Do you mean something like this?
QSettings *mySettings() { QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); QSettings *settings = new QSettings(path + "/Settings.ini", QSettings::IniFormat); return settings; } QSettings *ptSettings = mySettings(); ptSettings->beginGroup("Test"); ptSettings->setValue("Value", 3); ptSettings->endGroup(); delete ptSettings;
It seems to work.
-
Hi, also, instead of inheriting from QSettings, you could construct your own class with an encapsulated QSettings*, say something like this:
class MySettings { QSettings* pSettings; MySettings() : MySettings(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/Settings.ini") { } MySettings(QString sIniFileName) { pSettings = new QSettings(sIniFileName,QSettings::IniFormat); } ~MySettings() { delete pSettings; } void writeInt(QString sKeyName, int nValue) { pSettings->setValue(sKeyName,nValue); } }
And to use it, easiest is to declare it as a class instance, say in your MainWindow.h:
MySettings settings;
and then use it like this:
settings.writeInt("Test/Value",3);
-
I have a class name document_session as instance:
/// Header pointer & static: QPointer<DOC> DOC::_self = 0L; DOC *DOC::self(QObject *parent) { if (!_self) _self = new DOC(parent); return _self; } void DOC::setValue(const QString name, QVariant data) { QSettings session_s(QString("OasiClub"), QString("OasiEditor")); session_s.setValue(name, data); } QVariant DOC::value(const QString name) { QSettings session_s(QString("OasiClub"), QString("OasiEditor")); return session_s.value(name); } /// after read e write: const int uservoice = DOC::self(this)->value("MyVoicePref").toInt();
sample:
https://github.com/pehohlva/QOASIS/blob/master/src/app/doc_session.cpp
https://github.com/pehohlva/QOASIS/blob/master/src/app/doc_session.hhttps://github.com/pehohlva/QOASIS
https://sourceforge.net/projects/oasidoc/ -
Just with the purpose of helping someone else, I have implemented the following class based on the @hskoglund answer:
Header:
#ifndef MYSETTINGS_H #define MYSETTINGS_H #include <QVariant> #include <QSettings> #include <QStandardPaths> class MySettings { public: MySettings(QString fileName = "", QSettings::Format format = QSettings::IniFormat); ~MySettings(); void setValue(const QString &key, const QVariant &value); QVariant value(const QString &key, const QVariant &defaultValue = QVariant()); void beginGroup(const QString &prefix); void endGroup(); private: QSettings *pSettings_; }; #endif // MySettings_H
Code:
#include "mysettings.h" MySettings::MySettings(QString fileName, QSettings::Format format) { if( fileName.isEmpty() ) { QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); fileName = path + "/Settings.ini"; } pSettings_ = new QSettings(fileName, format); } MySettings::~MySettings() { delete pSettings_; } void MySettings::setValue(const QString &key, const QVariant &value) { pSettings_->setValue(key, value); } QVariant MySettings::value(const QString &key, const QVariant &defaultValue) { return pSettings_->value(key, defaultValue); } void MySettings::beginGroup(const QString &prefix) { pSettings_->beginGroup(prefix); } void MySettings::endGroup() { pSettings_->endGroup(); }
-
@patrik08
Hi
http://doc.qt.io/qt-5/qsettings.html#Format-enum
"on Unix, this means textual configuration files in INI format."so .ini is not only windows it seems :)
-
@KelvinSP said in How to create a class based on QSettings:
Just with the purpose of helping someone else, I have implemented the following class based on the @hskoglund answer
Just my 2 cents, but I don't understand why you would choose to encapsulate in this case rather than derive. Most of your code seems to act on the encapsulated
QSettings
object anyway, so I would see derivation as more natural (e.g. with derivation you canoverride
or callprotected
members, with encapsulation you cannot). Not meant as a criticism, there are often pros & cons, up to you.