QDatastream operate QPoint

  • Hello, I wrote a nibble code to test QDatastream, and set one break point at line 7.
    Environment is Qt5, QtCreator.
    But in QtCreator, when the program stopped at Line 7, in Local and Expression view, the tmp value is (0, 0), supposed to be (6,11).

    Dont' know why tmp is not (6,11).

    Many thanks in advance.

    1, QPoint pos(6,11);
    2, QPoint tmp;
    3, QByteArray datagram;

    4, QDataStream out(&datagram, QIODevice::ReadWrite);

    5, out << pos;
    6, out >> tmp;
    7, return 0;

  • Moderators

    @samzhcs1 That's because your data stream is at the end after you write your point into it (you can check that calling http://doc.qt.io/qt-5/qdatastream.html#atEnd). If you then read then there is nothing to read: is like reading from a non empty file while being at the end of the file.

  • This should work

    5, out << pos;
    6, out.device()->seek(0);
    7, out >> tmp;

  • Thanks, it works.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.