1
votes

Well The Problem is Pretty Complex. I am simplifying it with what I see in debugger.

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 inherits DG::Packet. and DG::Packet has << and >> operator overloads for QDataStream. DG::Packet serializes or Unserializedits data and then calls this->serialize() or unserialize() which is pure virtual Passing its stream.

qDebug() << pS.device()->size() << pS.device()->pos();
pS >> *msd;

These two Lines Print 156 0 in Terminal. which confirms there are 156 Bytes availableand current seek position is on the start of the File.

Then The Call goes to

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 is quint32 and the Call goes to

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

Here The Call goes to If . and it does setStatus(ReadPastEnd);. But why ? Its on the begening. and pos() returns 0 and there are still 156 Bytes to read.

1

1 Answers

2
votes

Cause you are doing

QDataStream pS(&file);

it should be

QDataStream pS(&file1);