QT tcp传输后端的文件夹结构到前端n问题

#我想做一个类似于百度云盘的客户端。
#使用QT 语言是C++
#如何实现将QFileSystemModel对象传输到前端中然后加载到treeview中去?

可以使用Qt的信号槽机制和Qt的网络模块,使用步骤如下:

1、创建一个QTcpServer对象,以便在后台等待来自前端的连接请求。您可以使用QTcpSocket来与前端通信。

2、将QFileSystemModel对象的数据传输到前端,您可以使用QDataStream类将模型的数据序列化并发送到前端。

3、在前端,您可以创建一个QTcpSocket对象,并使用connectToHost()函数连接到服务器。

4、一旦连接建立,您可以使用QDataStream读取从服务器发送的数据,并使用QTreeView加载模型数据。

下面是一个示例代码片段,请根据具体需求进行修改和扩展:

// 后端代码

// 创建 QTcpServer 对象
QTcpServer* server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &MyClass::handleNewConnection);

// 监听端口
server->listen(QHostAddress::Any, 12345);

void MyClass::handleNewConnection()
{
    // 有新的连接请求
    QTcpSocket* socket = server->nextPendingConnection();
    connect(socket, &QTcpSocket::readyRead, this, &MyClass::handleReadyRead);
}

void MyClass::handleReadyRead()
{
    // 从套接字中读取请求
    QTcpSocket* socket = static_cast<QTcpSocket*>(sender());

    if (!socket)
        return;

    // 从 QFileSystemModel 中获取数据,并序列化
    QByteArray serializedData;
    QDataStream stream(&serializedData, QIODevice::WriteOnly);
    stream << fileSystemModel->index(0, 0);
    
    // 将数据发送到前端
    socket->write(serializedData);
}
// 前端代码

// 连接到后端
QTcpSocket* socket = new QTcpSocket(this);
socket->connectToHost("127.0.0.1", 12345);

// 读取从服务器发送的数据
connect(socket, &QTcpSocket::readyRead, this, &MyClass::handleReadyRead);

void MyClass::handleReadyRead()
{
    // 从套接字中读取数据
    QByteArray data = socket->readAll();
    QDataStream stream(data);

    // 从数据流中反序列化 QFileSystemModel 的数据
    QModelIndex rootIndex;
    stream >> rootIndex;

    // 加载 QFileSystemModel 的数据到 QTreeView 中
    QTreeView* treeView = new QTreeView(this);
    QFileSystemModel* fileSystemModel = new QFileSystemModel(this);
    fileSystemModel->setRootPath("/");
    treeView->setModel(fileSystemModel);
    treeView->setRootIndex(rootIndex);
}
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7410802
  • 你也可以参考下这篇文章:Qt TCP 分包粘包的解决方法
  • 除此之外, 这篇博客: Qt中使用Tcp传输文件中的 三,客户端的实现 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 客户端主要就是解析服务端发送过来的头信息以及读文件即可
    代码及注释如下:

    #include "client.h"
    #include "ui_client.h"
    #include <QMessageBox>
    #include <QHostAddress>
    #include <QFileDialog>
    #include <QProgressBar>
    #include <QDebug>
    
    client::client(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::client)
    {
        ui->setupUi(this);
    
        tcpSocket = new QTcpSocket(this);
        isStart = 1;
    
        connect(tcpSocket, &QTcpSocket::readyRead,[=](){
                   /*取出接收的内容*/
            QByteArray buf = tcpSocket->readAll();
    
            if(isStart == 1)
            {
                isStart = 0;
    
                fileName = QString(buf).section("##", 0, 0);
                fileSize = QString(buf).section("##", 1, 1).toInt();
                RecvSize = 0;
    
                /*弹出接收文件的信息*/
                QString str = QString("接收的文件:[%1: %2KB]").arg(fileName).arg(fileSize);
                QMessageBox::information(this, "接收文件信息", str);
    
                /*打开文件*/
                QString path = QFileDialog::getSaveFileName(this,"save","../","souce(*.cpp)");
    
                if(path.isEmpty() == false)
                {
                    file.setFileName(path);
    
                    //打开文件
                    if(file.open(QIODevice::WriteOnly) == true)
                    {
    
    
    
                    }
                }
                /*
                file.setFileName(fileName);
                bool isOk = file.open(QIODevice::WriteOnly);
                if(isOk == false)
                {
                    qDebug() << "文件打开失败";
                }*/
            }
            if(isStart == 0)
            {
                /*文件信息*/
                qint64 len = file.write(buf.data());
                RecvSize += len;
                qDebug() << "执行到写数据的的地方";
                if(RecvSize == fileSize)
                {
                    file.close();
                    QMessageBox::information(this, "完成", "文件接收完成");
                    tcpSocket->disconnectFromHost();
                    tcpSocket->close();
                }
            }
        });
    }
    
    client::~client()
    {
        delete ui;
    }
    
    void client::on_connectButton_clicked()
    {
        /*获取IP和端口*/
        QString ip = ui->lineEditIP->text();
        quint64 port = ui->lineEditPort->text().toInt();
    
        tcpSocket->connectToHost(QHostAddress(ip),port);
    
    }
    
    void client::recvFile()
    {
        QString path = QFileDialog::getSaveFileName(this,"save","../","souce(*.cpp)");
    
        if(path.isEmpty() == false)
        {
            file.setFileName(path);
    
            //打开文件
            if(file.open(QIODevice::WriteOnly) == true)
            {
    
    
    
            }
        }
    
    }
    
    

    对于客户端来说,主要就是获取发送的头信息,不能和数据相互连包,其余和之前的Tcp数据传输一样。