Qt/C++ 将通用对象序列化为 QSettings

Qt/C++ Serialize generic object into QSettings

我在我的应用程序中有几次想将复合对象保存到 QSettings 中:

template <typename T>
class AwsProperty
{
public:
    AwsProperty(T value, quint64 timestamp){
        m_data = value;
        m_timestamp = timestamp;
    }
    AwsProperty(){}
    T value(){return m_data;}
    void update(T value,quint64 timestamp){m_data = value; m_timestamp = timestamp;}
    quint64 timestamp(){return m_timestamp;}

    void toQDataStream(QDataStream &dstream){dstream << m_data << m_timestamp;}
    void fromQDataStream(QDataStream &dstream){dstream >> m_data >> m_timestamp;}
private:
    quint64 m_timestamp;
    T m_data;
};

typedef AwsProperty<qint32> AwsPropertyInt32;
Q_DECLARE_METATYPE(AwsPropertyInt32)
typedef AwsProperty<quint32> AwsPropertyUint32;
Q_DECLARE_METATYPE(AwsPropertyUint32)
typedef AwsProperty<bool> AwsPropertyBool;
Q_DECLARE_METATYPE(AwsPropertyBool)

cpp 看起来像这样:

template <typename T>
QDataStream& operator<<(QDataStream& out, const AwsProperty<T>& classObj){
{
    classObj.toQDataStream(out);
    return out;
}
template <typename T>
QDataStream& operator>>(QDataStream& in, const AwsProperty<T>& classObj){
    classObj.fromQDataStream(in);
    return in;
}

我在我的主页注册了它们:

  qRegisterMetaTypeStreamOperators<AwsPropertyInt32>("AwsPropertyInt32");
    qRegisterMetaTypeStreamOperators<AwsPropertyUint32>("AwsPropertyUint32");
    qRegisterMetaTypeStreamOperators<AwsPropertyBool>("AwsPropertyBool");

我得到的错误是:

/opt/XXXXXXXXXX/0.9.99/sysroots/cortexa9hf-neon-XXXXXX-linux-gnueabi/usr/include/QtCore/qmetatype.h:810: error: no match for ‘operator<<’ (operand types are ‘QDataStream’ and ‘const AwsProperty<int>’)
         stream << *static_cast<const T*>(t);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

还有为什么没有为 QDataStreamQString 定义流操作符。

在实例化 QSettings 之前,您必须使用 qRegisterMetaTypeStreamOperators 将运算符注册到元类型系统。

此外,运算符和 fromQDataStreamtoQDataStream 方法的常量性略有错误(请参阅 link 了解所需的确切原型)。

编辑:

此代码适用于我:请注意,我更改了某些方法的常量并将运算符移至头文件,因为它们是模板。

awsproperty.h

#include <QDataStream>

template <typename T>
class AwsProperty
{
public:
    AwsProperty(T value, quint64 timestamp){
        m_data = value;
        m_timestamp = timestamp;
    }
    AwsProperty(){}
    T value(){return m_data;}
    void update(T value,quint64 timestamp){m_data = value; m_timestamp = timestamp;}
    quint64 timestamp(){return m_timestamp;}

    void toQDataStream(QDataStream &dstream) const {dstream << m_data << m_timestamp;}
    void fromQDataStream(QDataStream &dstream){dstream >> m_data >> m_timestamp;}
private:
    quint64 m_timestamp;
    T m_data;
};

template <typename T>
QDataStream& operator<<(QDataStream& out, const AwsProperty<T>& classObj) {
    classObj.toQDataStream(out);
    return out;
}

template <typename T>
QDataStream& operator>>(QDataStream& in, AwsProperty<T>& classObj) {
    classObj.fromQDataStream(in);
    return in;
}

typedef AwsProperty<qint32> AwsPropertyInt32;
Q_DECLARE_METATYPE(AwsPropertyInt32)
typedef AwsProperty<quint32> AwsPropertyUint32;
Q_DECLARE_METATYPE(AwsPropertyUint32)
typedef AwsProperty<bool> AwsPropertyBool;
Q_DECLARE_METATYPE(AwsPropertyBool)

main.cpp

#include "awsproperty.h"
#include <QSettings>
#include <QDebug>

int main(int argc, char *argv[])
{
    qRegisterMetaTypeStreamOperators<AwsPropertyInt32>("AwsPropertyInt32");
    qRegisterMetaTypeStreamOperators<AwsPropertyUint32>("AwsPropertyUint32");
    qRegisterMetaTypeStreamOperators<AwsPropertyBool>("AwsPropertyBool");
    QSettings set("/tmp/testsettings.conf");

    AwsPropertyInt32 a{-2, 100};
    set.setValue("property", QVariant::fromValue(a));
    auto val = set.value("property").value<AwsPropertyInt32>();
    qDebug() << val.value() << " " << val.timestamp();
}