编写和阅读自定义类到QSettings

编程入门 行业动态 更新时间:2024-10-23 07:39:22
本文介绍了编写和阅读自定义类到QSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要通过QSettings在注册表中存储自定义类的实例。从Qt的文档阅读后,我认为我已经实现了所需的功能,但我不设法保存任何东西。

I need to store instances of a custom class in the registry via QSettings. After reading from Qt's documentation, I think I have implemented the needed functions but I don't manage to save anything.

这是我的自订类别:

class SRSDefinition{ public: SRSDefinition(QString name, QString definition) : qsName(name), qsDefinition(definition) { qRegisterMetaType<SRSDefinition>("SRSDefinition"); qRegisterMetaTypeStreamOperators<SRSDefinition>("SRSDefinition"); } SRSDefinition(const SRSDefinition& original) : qsName(original.qsName), qsDefinition(original.qsDefinition) {} SRSDefinition() : qsName(""), qsDefinition(""){} ~SRSDefinition(); QString qsName; QString qsDefinition; private: friend QDataStream& operator << (QDataStream& out, const SRSDefinition& valeur) {out << valeur.qsName << valeur.qsDefinition; return out; } friend QDataStream& operator >> (QDataStream& in, SRSDefinition& valeur) {in >> valeur.qsName >> valeur.qsDefinition; return in;} }; Q_DECLARE_METATYPE(SRSDefinition) QDataStream& operator << (QDataStream& out, const SRSDefinition& Valeur); QDataStream& operator >> (QDataStream& in, SRSDefinition& Valeur);

在注册表中保存:

QSettings settings("Organization", "Application"); QVariant variant; variant.setValue(QVariant::fromValue<SRSDefinition>(m_srsSelected)); settings.setValue("Shared/RecentSRS", variant);

从注册表载入:

QSettings settings("Organization", "Application"); m_srsSelected = settings.value("Shared/RecentSRS").value<SRSDefinition>();

在注册表中创建一个新的键,但它包含十六进制代码,我认为只是名称的类...

A new key in the registry is created, but it contains hex code that I think is just the name of the class...

任何想法如何有效地存储值?

Any idea how to effectively store the values ?

推荐答案

使用 qRegisterMetaTypeStreamOperators< YourType& (YourType),以使用QVariant启用自定义类型的序列化/反序列化。该函数在Qt的元类型系统中注册你的类型的QDataStream输入/输出函数,以便在保存/加载包含你的自定义类型的变量时调用它们。

Use qRegisterMetaTypeStreamOperators<YourType>("YourType") to enable serialization/de-serialization of custom types with QVariant. This function registers your type's QDataStream input/output functions with Qt's metatype system so that it can invoke them when saving/loading a variant containing your custom type.

#include <QtCore/QDebug> #include <QtCore/QDataStream> #include <QtCore/QSettings> struct CustomType { QString name; int age; }; Q_DECLARE_METATYPE(CustomType); QDataStream& operator<<(QDataStream& out, const CustomType& v) { out << v.name << v.age; return out; } QDataStream& operator>>(QDataStream& in, CustomType& v) { in >> v.name; in >> v.age; return in; } int main(int,char**) { qRegisterMetaTypeStreamOperators<CustomType>("CustomType"); { CustomType t; t.name = "John Smith"; t.age = 42; QSettings s("config.ini", QSettings::IniFormat); s.setValue("user", QVariant::fromValue(t)); } { QSettings s("config.ini", QSettings::IniFormat); QVariant value = s.value("user"); CustomType t = value.value<CustomType>(); qDebug() << t.name << t.age; } return 0; }

更多推荐

编写和阅读自定义类到QSettings

本文发布于:2023-11-24 03:29:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   QSettings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!