开发者

problem with QDataStream & QDataStream::operator>> ( char *& s )

 QFile msnLogFile(item->data(Qt:开发者_如何转开发:UserRole).toString());
 QDataStream logDataStream;
    if(msnLogFile.exists()){
        msnLogFile.open(QIODevice::ReadOnly);
        logDataStream.setDevice(&msnLogFile);
        QByteArray logBlock;
        logDataStream >> logBlock;
    }

This code doesnt work. The QByte that results is empty. Same thing if I use a char* . Oddely enough the same code works in another program. Im tying to find the difference between both. This works if i use int,uint, quint8, etc


Assuming msnLogFile was not previously created using a QDataStream (if it was, then ignore this answer completely), you don't want to use the >> operator.

The reason is that when QDataStream is writing strings, it prepends the length of the string to the output bytes. This allows another QDataStream to read it back in with the correct length and get the same result. Hence, why int, qint8, etc work correctly; there's no prepended size, it's just the raw data.

If the contents of msnLogFile are strictly text, you need to pass the QIODevice::Text flag to open and use QIODevice::readLine() or QIODevice::readAll(), however if it's binary data you'd have to use QDataStream::readRawData() and read the data back out in the correct order with correct sizes.


I'd venture to guess it's because you're not specifying the protocol version. You should call setVersion() to ensure that multiple machines, which may be using different versions of Qt, all use the same protocol version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜