我的应用程序中有几次要在其中将复合对象保存到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);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
还有为什么未为
QDataStream
和QString
定义流运算符。 最佳答案
在实例化qRegisterMetaTypeStreamOperators
之前,必须使用 QSettings
将操作符注册到元类型系统。
同样,运算符的常量性以及fromQDataStream
和toQDataStream
方法也有一些错误(请参阅链接以获取所需的确切原型(prototype))。
编辑:
该代码对我有用:请注意,由于它们是模板,因此我更改了某些方法的常量并将操作符移至头文件。
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();
}
关于c++ - Qt/C++将通用对象序列化为QSettings,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60816176/