qt客户端打包,QT - QFileSystemModel在服务器客户端程序

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

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值