mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2025-07-05 17:55:25 +08:00
update
This commit is contained in:
40
example/src/helper/SettingsHelper.cpp
Normal file
40
example/src/helper/SettingsHelper.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "SettingsHelper.h"
|
||||
|
||||
#include <QDataStream>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
QVariant SettingsHelper::get(const QString& key){
|
||||
const QByteArray data = m_settings->value(key).toByteArray();
|
||||
if (data.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
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";
|
||||
const QString iniFilePath = fileInfo.dir().path() + "/" + iniFileName;
|
||||
qDebug()<<iniFilePath;
|
||||
m_settings.reset(new QSettings(iniFilePath, QSettings::IniFormat));
|
||||
}
|
33
example/src/helper/SettingsHelper.h
Normal file
33
example/src/helper/SettingsHelper.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef SETTINGSHELPER_H
|
||||
#define SETTINGSHELPER_H
|
||||
|
||||
#include <QtCore/qobject.h>
|
||||
#include <QtQml/qqml.h>
|
||||
#include <QSettings>
|
||||
#include <QScopedPointer>
|
||||
#include <QFileInfo>
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include "src/singleton.h"
|
||||
|
||||
class SettingsHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
explicit SettingsHelper(QObject* parent = nullptr);
|
||||
public:
|
||||
SINGLETONG(SettingsHelper)
|
||||
~SettingsHelper() override;
|
||||
void init(char *argv[]);
|
||||
Q_INVOKABLE void saveRender(const QString& render){save("render",render);}
|
||||
Q_INVOKABLE QString getReander(){return get("render").toString();}
|
||||
Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);}
|
||||
Q_INVOKABLE int getDarkMode(){return get("darkMode").toInt(0);}
|
||||
private:
|
||||
void save(const QString& key,QVariant val);
|
||||
QVariant get(const QString& key);
|
||||
private:
|
||||
QScopedPointer<QSettings> m_settings;
|
||||
};
|
||||
|
||||
#endif // SETTINGSHELPER_H
|
Reference in New Issue
Block a user