Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

Here's my custom class :

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);

Saving in the registry :

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

Loading from the registry :

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... enter image description here

Any idea how to effectively store the values ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...