Question

 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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top