问题很复杂。我用调试器中的内容简化了它。
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QDataStream psudoStream(&file);
psudoStream.setVersion(QDataStream::Qt_4_7);
psudoStream << *packet;// Data is being serialized Properly as I can see The File in Hex Editor
file.close();
QFile file1(fileName);
file1.open(QIODevice::ReadOnly);
QDataStream pS(&file);
pS.setVersion(QDataStream::Qt_4_7);
DG::MouseEventsPacket* msd = new DG::MouseEventsPacket;
qDebug() << pS.device()->size() << pS.device()->pos();
pS >> *msd;//But the Problem Starts from Here
file1.close();
DG :: MouseEventsPacket继承DG :: Packet。和DG :: Packet有&lt;&lt;和&gt;&gt; QDataStream的运算符重载。 DG :: Packet序列化或Unserializedits数据然后调用this-&gt; serialize()或unserialize()这是纯虚拟传递其流。
qDebug() << pS.device()->size() << pS.device()->pos();
pS >> *msd;
这两行在终端中打印156 0
。确认有156个字节可用,当前搜索位置在文件的开头。
然后呼叫转到
QDataStream& DG::operator>>(QDataStream& stream, Packet& packet){
int _type;
stream >> packet._state >> _type;
packet._type = (DG::Packet::PacketType)_type;
return packet.unserialize(stream);
}
packet._state是quint32,呼叫转到
QDataStream &QDataStream::operator>>(qint32 &i){
i = 0;
CHECK_STREAM_PRECOND(*this)
if (dev->read((char *)&i, 4) != 4) {
i = 0;
setStatus(ReadPastEnd);
} else {
if (!noswap) {
i = qbswap(i);
}
}
return *this;
}
这里呼叫转到If。它确实是setStatus(ReadPastEnd);.但为什么 ?它在酝酿中。和pos()返回0,仍然有156个字节要读。
答案 0 :(得分:2)
因为你正在做
QDataStream pS(&file);
应该是
QDataStream pS(&file1);