update SettingsHelper

This commit is contained in:
zhuzichu 2023-12-08 17:30:50 +08:00
parent 7086df10f0
commit 33c203fcb3
2 changed files with 9 additions and 17 deletions

View File

@ -12,23 +12,15 @@ 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);
m_settings->setValue(key, val);
}
QVariant SettingsHelper::get(const QString& key,QVariant def){
const QByteArray data = m_settings->value(key).toByteArray();
if (data.isEmpty()) {
return def;
QVariant data = m_settings->value(key);
if (!data.isNull() && data.isValid()) {
return data;
}
QDataStream stream(data);
stream.setVersion(QDataStream::Qt_5_6);
QVariant val;
stream >> val;
return val;
return def;
}
void SettingsHelper::init(char *argv[]){

View File

@ -20,13 +20,13 @@ public:
~SettingsHelper() override;
void init(char *argv[]);
Q_INVOKABLE void saveRender(const QVariant& render){save("render",render);}
Q_INVOKABLE QVariant getRender(){return get("render");}
Q_INVOKABLE QString getRender(){return get("render").toString();}
Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);}
Q_INVOKABLE QVariant getDarkMode(){return get("darkMode",QVariant(0));}
Q_INVOKABLE int getDarkMode(){return get("darkMode",QVariant(0)).toInt();}
Q_INVOKABLE void saveVsync(bool vsync){save("vsync",vsync);}
Q_INVOKABLE QVariant getVsync(){return get("vsync",QVariant(true));}
Q_INVOKABLE bool getVsync(){return get("vsync",QVariant(true)).toBool();}
Q_INVOKABLE void saveUseSystemAppBar(bool useSystemAppBar){save("useSystemAppBar",useSystemAppBar);}
Q_INVOKABLE QVariant getUseSystemAppBar(){return get("useSystemAppBar",QVariant(false));}
Q_INVOKABLE bool getUseSystemAppBar(){return get("useSystemAppBar",QVariant(false)).toBool();}
private:
void save(const QString& key,QVariant val);
QVariant get(const QString& key,QVariant def={});