I have a list of files and I'd like to serialize the file info for every file and send it through socket.

I saw it's possible to serialize like this for example:

QByteArray ba;
QDataStream ds(&ba);
ds << my_stringlist;

QByteArray ba;
QDataStream ds(&ba);
ds >> my_stringlist;

but I couldn't find support for QFileInfo. Is it possible to serialize this Qt data type? Is there any way to get an easy full serialization of this type or I just need to break up the data into primitive units?

有帮助吗?

解决方案

There is no standard way to do that. You can define your custom QDataStream operators as showed in this answer, or you can write your own functions to convert QFileInfo to QVariant and back, and use QVariant serialization. In all these ways you need to break up the data into primitive units, yes.

However I think serializing QFileInfo is pointless. You should use QFileInfo::absoluteFilePath() to get the file's path and serialize that path instead. A new QFileInfo object can be easily constructed from that path if your receiving code is running on the same machine.

If your code is running on the other machine, you couldn't use deserialized QFileInfo even if it would be possible. It's because QFileInfo may or may not store information about file. When you run e.g. QFileInfo::isFile, it may make a request to the underlying file system.

So I think it's better to request all required data from QFileInfo add send this data instead of sending QFileInfo. Or you can just send the absolute file path.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top