2023-09-17 20:36:33 +08:00
|
|
|
#include "SettingsHelper.h"
|
|
|
|
|
|
|
|
#include <QDataStream>
|
2023-09-29 19:30:22 +08:00
|
|
|
#include <QStandardPaths>
|
2023-09-17 20:36:33 +08:00
|
|
|
|
|
|
|
SettingsHelper::SettingsHelper(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsHelper::~SettingsHelper() = default;
|
|
|
|
|
|
|
|
void SettingsHelper::save(const QString& key,QVariant val)
|
|
|
|
{
|
|
|
|
QByteArray data = {};
|
|
|
|
QDataStream stream(&data, QIODevice::WriteOnly);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_6);
|
|
|
|
stream << val;
|
|
|
|
m_settings->setValue(key, data);
|
|
|
|
}
|
|
|
|
|
2023-10-17 22:18:12 +08:00
|
|
|
QVariant SettingsHelper::get(const QString& key,QVariant def){
|
2023-09-17 20:36:33 +08:00
|
|
|
const QByteArray data = m_settings->value(key).toByteArray();
|
|
|
|
if (data.isEmpty()) {
|
2023-10-17 22:18:12 +08:00
|
|
|
return def;
|
2023-09-17 20:36:33 +08:00
|
|
|
}
|
|
|
|
QDataStream stream(data);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_6);
|
|
|
|
QVariant val;
|
|
|
|
stream >> val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SettingsHelper::init(char *argv[]){
|
|
|
|
auto applicationPath = QString::fromStdString(argv[0]);
|
|
|
|
const QFileInfo fileInfo(applicationPath);
|
|
|
|
const QString iniFileName = fileInfo.completeBaseName() + ".ini";
|
2023-09-29 19:30:22 +08:00
|
|
|
const QString iniFilePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/" + iniFileName;
|
2023-09-26 20:57:52 +08:00
|
|
|
qDebug()<<"Application configuration file path->"<<iniFilePath;
|
2023-09-17 20:36:33 +08:00
|
|
|
m_settings.reset(new QSettings(iniFilePath, QSettings::IniFormat));
|
|
|
|
}
|