QT - QFileSystemModel on server-client program

1.6k views Asked by At

I am working on a server-client QT project that allows to transfer files between server and client. Being a GUI project, I want to display the server's file system on client program, like a file explorer.

The question is : is there a way to send the QFileSystemModel or QFileSystemWatcher from server to client, or to display the server's system file on client side? Thank you.

2

There are 2 answers

0
Mihai Bişog On BEST ANSWER

The best approach to tackle this problem is to implement a serialization technique for your object(s). Once that's done you'd need to implement rules to exchange such serialized objects at the communication protocol level.

For instance, you could serialize a path like this:

file size - 8bytes; path flags - 1byte; path size - 2bytes; actual path - `path size` bytes;

In the file size field you'd save the size of the file pointed by the path, in flags field you'd save additional information about the file (eg. if it's a directory/file etc.;) in the path size the size of the path and in the actual path field, (obviously) the path.

For example, say I want to distinguish between files/directories by setting bit 0 in the flags field: 1 for files, 0 for directories. A directory path that holds 512MB of files such as '/abc/d/efg' would be serialized as (using | as field delimiter):

536870912 | 0 | 10 | /abc/d/efg

To serialize a directory tree you'd just serialize every file in in there and store the bytes in a vector. To serialize the vector you'd just add a prefix to it (say 4 bytes) specifying the number of files it contains; For example, a stream in this format:

2 | (536870912 | 0 | 10 | /abc/d/efg), (536870912 | 0 | 10 | /abc/d/efh)

Would tell me that I have 2 file path entries both of size 512MB, both directories one with name /abc/d/efg/ and one with name /abc/d/efh.

Now you know how to serialize/deserialize a directory tree. To send it over to the other party I'd first prefix my serialized object with a unique message id type (so that the other party knows what it is receiving) and then send it over.

So in the end, given the two entries above, and assuming I prefix this type of serialized object with 0x00 the final stream would look like:

0 | 2 | (536870912 | 0 | 10 | /abc/d/efg), (536870912 | 0 | 10 | /abc/d/efh)

2
user1610743 On

You could try whether using QDataStream works with the model class. A really good example can be found here:

YouTube: C++ Qt 85 - Binary IO basic object serialization