C++ QVariant反序列化提供的值无效
我有以下要序列化和反序列化的类:C++ QVariant反序列化提供的值无效,c++,qt,serialization,C++,Qt,Serialization,我有以下要序列化和反序列化的类: #include <QObject> #include <QDataStream> #include <QVariant> /** * @brief The type used to store the \ref CComPacket::Cmd enum */ using CmdType = quint8; /** * @brief A smart container for the TCP packets
#include <QObject>
#include <QDataStream>
#include <QVariant>
/**
* @brief The type used to store the \ref CComPacket::Cmd enum
*/
using CmdType = quint8;
/**
* @brief A smart container for the TCP packets
*/
class CComPacket : public QObject
{
Q_OBJECT
public:
/**
* @brief Provides the command information for the packet (tells how
* the data is supposed to be parsed).
*/
enum class Cmd: CmdType
{
Invalid,
ReadName,
GiveName,
};
explicit CComPacket(QObject *parent = nullptr);
/**
* @brief The ostream (<<) overloading
* @param out: The data stream to which the \p cpp class is going to
* be serialized.
* @param ccp: the class to be serialized into the \p out.
* @return reference to \p out.
*/
friend QDataStream& operator<<(QDataStream& out, const CComPacket& ccp)
{
out << static_cast<CmdType>(ccp.m_cmd) << ccp.m_data;
return out;
}
/**
* @brief The istream (>>) overloading
* @param in: The serialized data that will be deserialized to the \p cpp
* @param ccp: deserialized data goes here.
* @return The \p in reference.
*/
friend QDataStream& operator>>(QDataStream& in, CComPacket& ccp)
{
CmdType tmp;
in >> tmp >> ccp.m_data;
ccp.m_cmd = static_cast<Cmd>(tmp);
return in;
}
/**
* @brief The parsing command
*/
Cmd m_cmd = Cmd::Invalid;
/**
* @brief The data to be parsed
*/
QVariant m_data;
};
#包括
#包括
#包括
/**
*@brief用于存储\ref CComPacket::Cmd枚举的类型
*/
使用CmdType=8;
/**
*@brief用于TCP数据包的智能容器
*/
C类公司:公共QoObject
{
Q_对象
公众:
/**
*@brief提供数据包的命令信息(说明如何
*数据应该被解析)。
*/
枚举类Cmd:CmdType
{
无效的
ReadName,
名字,
};
显式CComPacket(QObject*parent=nullptr);
/**
*@简要介绍ostream(tmp>>ccp.m_数据;
ccp.m_cmd=静态施法(tmp);
返回;
}
/**
*@brief解析命令
*/
Cmd m_Cmd=Cmd::无效;
/**
*@简要说明要分析的数据
*/
qm_数据;
};
我正试图用以下代码来测试这一点:
CComPacket tmp;
tmp.m_cmd = CComPacket::Cmd::GiveName;
tmp.m_data = 5.112233;
QDataStream str;
str << tmp;
CComPacket tmp2;
str >> tmp2;
ccompackettmp;
tmp.m_cmd=CComPacket::cmd::GiveName;
tmp.m_数据=5.112233;
QDataStream-str;
str>tmp2;
调试时,我看到
tmp2
的m_cmd
成员已正确反序列化,但m_data
对象仍然无效。从文档中我了解到QVariant
默认支持序列化。我缺少什么?为了实现这一点,我必须合并QBytarray
必须用作QDataStream
的参数,因为已经需要物理容器。我计划在后面的步骤中将数据序列化为字节,因此这只是让一切变得更简单。我添加了以下成员函数:
QByteArray serialize();
static CComPacket deserialize(QByteArray& ba);
QByteArray CComPacket::serialize()
{
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << *this;
return ba;
}
CComPacket CComPacket::deserialize(QByteArray& ba)
{
QDataStream ds(&ba, QIODevice::ReadOnly);
CComPacket cp;
ds >> cp;
return cp;
}
在您的测试代码中,QDataStream str没有在任何设备上运行,因此无法工作。这只是本例中的一个疏忽,还是真实的代码?它是真实的代码。我忘记了为流提供实际的缓冲区,正如我在回答中所说的。谢谢。
CComPacket tmp;
tmp.m_cmd = CComPacket::Cmd::GiveName;
tmp.m_data = 5.112233;
QByteArray ba = tmp.serialize();
CComPacket tmp2 = CComPacket::deserialize(ba);