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.
解决方案
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)
本文探讨了如何在QT的客户端-服务器项目中实现文件系统的远程展示,类似于文件浏览器。通过序列化技术将服务器的文件系统信息转换为可传输的数据,并在通信协议层面定义规则进行交换。具体方法包括:文件大小、路径标志、路径大小和实际路径等字段的序列化,以及如何通过消息ID区分不同类型的序列化对象。该方案提供了将服务器目录树结构发送到客户端的方法。

被折叠的 条评论
为什么被折叠?



