//string#include // cout#include #include FileSetDialog::FileSetDialog(QWidget *parent): ._ui->buttonbox->button(qdialogbu">搜索查看编辑修改首页UNITYNODEJSPYTHONAIGITPHPGOCEF3JAVAHTMLCSS搜索从前慢现在也慢 这个屌丝很懒,什么也没留下! 关注作者热门标签jqueryHTMLCSSPHPASPPYTHONGOAICC++C#PHOTOSHOPUNITYiOSandroidvuexml爬虫SEOLINUXWINDOWSJAVAMFCCEF3CADNODEJSGITPyppeteerarticle热门文章1Android Studio 模拟器的选择和安装_android studio 是用什么安卓模拟器2python的opencv最最基础初学3ubuntu实战001:将ISO文件挂载配置成apt本地源_ubuntu18配置iso本地apt源4Android实现View截图并保存到相册_android保存view图片5Android Studio之Gradle手动下载6C语言中int到float的强制类型转换7这个国产软件远超微软 GitHub Copilot,让我的编码效率直接翻倍8layui 数据表格请求传参_Layui table 组件的使用之初始化加载数据、数据刷新表格、传参数...9Ubuntu 18.04双系统安装教程-超详细(原系统Win7,解决安装完成后启动Ubuntu进入GRUB的问题)_boot in grub2 mode10Macos Monterery Intel打开Andriod Studio失败闪退意外退出的一次解决记录_macos android studio 连移动硬盘闪退当前位置: article > 正文 QT 实现选择文件并上传显示进度条,tcp传输_ui->buttonbox->button(qdialogbuttonbox::cancel)->s 作者:从前慢现在也慢 | 2024-03-18 16:37:46 赞踩ui->buttonbox->button(qdialogbuttonbox::cancel)->settext("关闭"); Qt 实现选择文件 上传,进度条,客户端 .cpp: #include "filesetdialog.h"#include "ui_filesetdialog.h"#include <string> //string#include <iostream> // cout#include <QString>#include <QTextCodec>FileSetDialog::FileSetDialog(QWidget *parent): QDialog(parent), ui(new Ui::FileSetDialog){ ui->setupUi(this); this->initTCP(); //文件传送相关变量初始化 //文件传送相关变量初始化 perDataSize = 64 * 1024; totalBytes = 0; bytestoWrite = 0; bytesWritten = 0; bytesReceived = 0; filenameSize = 0; ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("关闭"); connect(this->ui->pushButton_openFile, SIGNAL(clicked()), this, SLOT(selectFile())); connect(this->ui->pushButton_sendFile, SIGNAL(clicked()), this, SLOT(sendFile()));} FileSetDialog::~FileSetDialog(){ delete ui;} void FileSetDialog::initTCP(){ this->tcpSocket = new QTcpSocket(this);} void FileSetDialog::disconnectServer(){ } void FileSetDialog::selectFile(){ this->fileSocket = new QTcpSocket(this); fileSocket->abort(); fileSocket->connectToHost("127.0.0.1", 8888); // 文件传送进度条更新 connect(fileSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(updateFileProgress(qint64))); this->ui->progressBar->setValue(0); this->filename = QFileDialog::getOpenFileName(this, "Open a file", "/", "files (*)"); ui->lineEdit_filename->setText(filename);//文件名} void FileSetDialog::sendFile(){ this->localFile = new QFile(filename); if (!localFile->open(QFile::ReadOnly)) { ui->textEdit->setText(tr("FileSetDialog:open file error!")); return; } // 获取文件大小 this->totalBytes = localFile->size(); // qDebug() << "this->totalBytes====" << this->totalBytes; //QByteArray t = localFile->readAll();//获取文件内容展示出来 //ui->textEdit->setText(QString(t)); QDataStream sendout(&outBlock, QIODevice::WriteOnly); sendout.setVersion(QDataStream::Qt_4_8); QString currentFileName = filename.right(filename.size() - filename.lastIndexOf('/') - 1); qDebug() << sizeof(currentFileName); // 保留总代大小信息空间、文件名大小信息空间、文件名 sendout << qint64(0) << qint64(0) << currentFileName; totalBytes += outBlock.size(); sendout.device()->seek(0); sendout << totalBytes << qint64((outBlock.size() - sizeof(qint64)* 2)); bytestoWrite = totalBytes - fileSocket->write(outBlock); outBlock.resize(0);} void FileSetDialog::updateFileProgress(qint64 numBytes){ // 已经发送的数据大小 bytesWritten += (int)numBytes; // 如果已经发送了数据 if (bytestoWrite > 0) { outBlock = localFile->read(qMin(bytestoWrite, perDataSize)); // 发送完一次数据后还剩余数据的大小 bytestoWrite -= ((int)fileSocket->write(outBlock)); // 清空发送缓冲区 outBlock.resize(0); } else localFile->close(); // qDebug() << "totalBytes==" << totalBytes << "bytesWritten== " << bytesWritten; // 更新进度条 this->ui->progressBar->setMaximum(totalBytes); this->ui->progressBar->setValue(bytesWritten); // 如果发送完毕 if (bytesWritten >= totalBytes) { localFile->close(); disconnect(fileSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(updateFileProgress(qint64))); delete localFile; bytesWritten = 0; //fileSocket->close(); }} .h: #ifndef FILESETDIALOG_H#define FILESETDIALOG_H #include <QDialog>#include <QtWidgets/QWidget>#include <QtNetwork>#include <QHostAddress>#include <QMessageBox>#include <QDataStream>#include <QByteArray>#include <QDebug>#include <QDateTime>#include <QFile>#include <QFileDialog>#include <QAbstractButton> namespace Ui {class FileSetDialog;} class FileSetDialog : public QDialog{ Q_OBJECT public: explicit FileSetDialog(QWidget *parent = nullptr); ~FileSetDialog(); void initTCP(); void newConnect(); private slots: // 与服务器断开连接 void disconnectServer(); // 浏览文件 void selectFile(); // 发送文件 void sendFile(); // 更新文件发送进度 void updateFileProgress(qint64); private: Ui::FileSetDialog *ui; QTcpSocket *tcpSocket; QTcpSocket *fileSocket; // 文件传送 QFile *localFile; // 文件大小 qint64 totalBytes; //文件总字节数 qint64 bytesWritten; //已发送的字节数 qint64 bytestoWrite; //尚未发送的字节数 qint64 filenameSize; //文件名字的字节数 qint64 bytesReceived; //接收的字节数 ///每次发送数据大小 qint64 perDataSize; QString filename; ///数据缓冲区 QByteArray inBlock; QByteArray outBlock; QString selectPath ="";}; #endif // FILESETDIALOG_H 服务端: .cpp: #include "filemanagment.h"#include <QDataStream>#include <QMessageBox>#include <QString>#include <QByteArray>#include <QApplication> #include <QTextCodec> FileManagment * FileManagment::widget=nullptr;FileManagment::FileManagment(QWidget *parent) : QWidget(parent){ // 文件传送套接字 this->filesocket = new QTcpSocket(this); this->fileserver = new QTcpServer(this); this->fileserver->listen(QHostAddress::Any,8888); connect(this->fileserver,SIGNAL(newConnection()),this,SLOT(acceptFileConnection())); // 文件传送相关变量初始化 bytesReceived = 0; totalBytes = 0; filenameSize = 0;} FileManagment *FileManagment::getFileManagment(){ if(widget==nullptr) { widget = new FileManagment(); } return widget;} FileManagment::~FileManagment(){ close();// delete m_sever;} void FileManagment::acceptFileConnection(){ bytesWritten = 0; //每次发送数据大小为64kb perDataSize = 64*1024; this->filesocket = this->fileserver->nextPendingConnection(); //接受文件 connect(filesocket,SIGNAL(readyRead()),this,SLOT(updateFileProgress())); connect(filesocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(updateFileProgress(qint64))); connect(filesocket,SIGNAL(bytesWritten(qint64)),this,SLOT(displayError(QAbstractSocket::SocketError socketError))); qDebug() << "acceptFileConnection ";} void FileManagment::updateFileProgress(){ QDataStream inFile(this->filesocket); inFile.setVersion(QDataStream::Qt_4_8); //如果接收到的数据小于16个字节,保存到来的文件头结构 if(bytesReceived <= sizeof(qint64)*2) { if((filesocket->bytesAvailable()>=(sizeof(qint64))*2) && (filenameSize==0)) { inFile>>totalBytes>>filenameSize; bytesReceived += sizeof(qint64)*2; } if((filesocket->bytesAvailable()>=filenameSize) && (filenameSize != 0)) { inFile>>filename; bytesReceived += filenameSize; //创建同名文件 QString filePath = QApplication::applicationDirPath() + "/../../S_Video_v3/fileData"; qDebug() << " 文件夹filePath!" << filePath; QDir dir(filePath); // QDir *temp = new QDir; bool exist = dir.exists(filePath); if(exist) qDebug() << "文件夹已经存在!"; else { bool ok = dir.mkdir(filePath); if( ok ) qDebug() << "文件夹不存在,创建成功!"; } filePath = dir.absolutePath() + QString("/%1").arg(filename); localFile = new QFile(filePath); // // 接收的文件放在指定目录下// filename = "ClientData/"+filename;// localFile = new QFile(filename); if(!localFile->open(QFile::WriteOnly)) { qDebug()<<"Server::open file error!"; return; } } else return; } // /如果接收的数据小于总数据,则写入文件 if(bytesReceived < totalBytes) { bytesReceived += filesocket->bytesAvailable(); inBlock = filesocket->readAll(); localFile->write(inBlock); inBlock.resize(0); } // 数据接收完成时 if(bytesReceived == totalBytes) { // this->ui.browser->append("Receive file successfully!"); bytesReceived = 0; totalBytes = 0; filenameSize = 0; localFile->close(); //filesocket->close(); }} void FileManagment::displayError(QAbstractSocket::SocketError socketError){ qDebug()<< "displayError--" << socket->errorString(); socket->close();} .h: #ifndef FILEMANAGMENT_H#define FILEMANAGMENT_H#include <QString>#include <QDebug>#include <QTcpServer>#include <QTcpSocket>#include <QWidget>#include <QProcess>#include <iostream>#include <QThread>using namespace std; class FileManagment: public QWidget{ Q_OBJECTpublic: explicit FileManagment(QWidget *parent = nullptr); ~FileManagment(); static FileManagment * getFileManagment(); QTcpServer *server; QTcpSocket *socket; QTcpServer *fileserver; QTcpSocket *filesocket; private slots: void acceptFileConnection(); void updateFileProgress(); void displayError(QAbstractSocket::SocketError socketError); private: static FileManagment *widget;// 传送文件相关变量 qint64 totalBytes; qint64 bytesReceived; qint64 bytestoWrite; qint64 bytesWritten; qint64 filenameSize; QString filename; ///每次发送数据大小 qint64 perDataSize; QFile *localFile;// 本地缓冲区 QByteArray inBlock; QByteArray outBlock;}; #endif // FILEMANAGMENT_H 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/264367推荐阅读article《ElementPlus 与 ElementUI 差异集合》el-input 和 el-button...el-input 和 el-button 属性 size 有变化《ElementPlus 与 ElementUI 差异集... 赞踩article<el-input>标签和<el-button>标签宽度不一致(ElementUI样式未使用)_标签和标签宽度不一致(ElementUI样式未使用)标签宽度未自适应_<el-input type="button ... 赞踩article鸿蒙Harmony(五)ArkUI---基础组件:Text、TextInput、Button、Sli...1.设置文案在资源文件中添加对应的文本资源2.属性设置效果。_鸿蒙arkts textinput鸿蒙arkts text... 赞踩article鸿蒙开发实战项目(二十四):ArkUI常用布局容器对齐方式(ArkTS)_arkts button ...本篇Codelab主要介绍Flex、Column、Row和Stack这四种布局容器内子组件对齐方式的设置方法。通过本Co... 赞踩articleAndroid Button#background 在MaterialComponents主题下无效...方案很简单,一旦涉及自己的项目那可能有点麻烦,诸如修改为Bridge主题,app的好多UI变动了之类的,我们项目中碰到的... 赞踩article居中显示并旋转 android Button 里的属性drawableLeft_button dra...如图,点击同步按钮,同步图片要旋转起来,直到同步完毕。有一个容易实现的方法,就叫“方法1”吧(下面会用的),一个Line... 赞踩articleo" href="/w/繁依Fanyi0/article/detail/263318" target="_blank">NodeMCU实现远程控制LED灯_"href=\"/led2_off\"><button>o...o" href="/w/繁依Fanyi0/article/detail/263318" target="_blank">用NodeMCU的WIFI功能来演示一个最简单的物联网应用_"off""href=\"/led2_off\">o" href="/article/detail/63318" target="_blank">[详细] --> 赞踩相关标签elementuivue.js前端javascript鸿蒙harmonyosOpenHarmony鸿蒙开发实战项目ArkTS常用布局容器对齐方式androiduiMaterialButton背景无效
赞
踩
Qt 实现选择文件 上传,进度条,客户端 .cpp:
#include "filesetdialog.h"#include "ui_filesetdialog.h"#include <string> //string#include <iostream> // cout#include <QString>#include <QTextCodec>FileSetDialog::FileSetDialog(QWidget *parent): QDialog(parent), ui(new Ui::FileSetDialog){ ui->setupUi(this); this->initTCP(); //文件传送相关变量初始化 //文件传送相关变量初始化 perDataSize = 64 * 1024; totalBytes = 0; bytestoWrite = 0; bytesWritten = 0; bytesReceived = 0; filenameSize = 0; ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("关闭"); connect(this->ui->pushButton_openFile, SIGNAL(clicked()), this, SLOT(selectFile())); connect(this->ui->pushButton_sendFile, SIGNAL(clicked()), this, SLOT(sendFile()));} FileSetDialog::~FileSetDialog(){ delete ui;} void FileSetDialog::initTCP(){ this->tcpSocket = new QTcpSocket(this);} void FileSetDialog::disconnectServer(){ } void FileSetDialog::selectFile(){ this->fileSocket = new QTcpSocket(this); fileSocket->abort(); fileSocket->connectToHost("127.0.0.1", 8888); // 文件传送进度条更新 connect(fileSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(updateFileProgress(qint64))); this->ui->progressBar->setValue(0); this->filename = QFileDialog::getOpenFileName(this, "Open a file", "/", "files (*)"); ui->lineEdit_filename->setText(filename);//文件名} void FileSetDialog::sendFile(){ this->localFile = new QFile(filename); if (!localFile->open(QFile::ReadOnly)) { ui->textEdit->setText(tr("FileSetDialog:open file error!")); return; } // 获取文件大小 this->totalBytes = localFile->size(); // qDebug() << "this->totalBytes====" << this->totalBytes; //QByteArray t = localFile->readAll();//获取文件内容展示出来 //ui->textEdit->setText(QString(t)); QDataStream sendout(&outBlock, QIODevice::WriteOnly); sendout.setVersion(QDataStream::Qt_4_8); QString currentFileName = filename.right(filename.size() - filename.lastIndexOf('/') - 1); qDebug() << sizeof(currentFileName); // 保留总代大小信息空间、文件名大小信息空间、文件名 sendout << qint64(0) << qint64(0) << currentFileName; totalBytes += outBlock.size(); sendout.device()->seek(0); sendout << totalBytes << qint64((outBlock.size() - sizeof(qint64)* 2)); bytestoWrite = totalBytes - fileSocket->write(outBlock); outBlock.resize(0);} void FileSetDialog::updateFileProgress(qint64 numBytes){ // 已经发送的数据大小 bytesWritten += (int)numBytes; // 如果已经发送了数据 if (bytestoWrite > 0) { outBlock = localFile->read(qMin(bytestoWrite, perDataSize)); // 发送完一次数据后还剩余数据的大小 bytestoWrite -= ((int)fileSocket->write(outBlock)); // 清空发送缓冲区 outBlock.resize(0); } else localFile->close(); // qDebug() << "totalBytes==" << totalBytes << "bytesWritten== " << bytesWritten; // 更新进度条 this->ui->progressBar->setMaximum(totalBytes); this->ui->progressBar->setValue(bytesWritten); // 如果发送完毕 if (bytesWritten >= totalBytes) { localFile->close(); disconnect(fileSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(updateFileProgress(qint64))); delete localFile; bytesWritten = 0; //fileSocket->close(); }}
.h:
#ifndef FILESETDIALOG_H#define FILESETDIALOG_H #include <QDialog>#include <QtWidgets/QWidget>#include <QtNetwork>#include <QHostAddress>#include <QMessageBox>#include <QDataStream>#include <QByteArray>#include <QDebug>#include <QDateTime>#include <QFile>#include <QFileDialog>#include <QAbstractButton> namespace Ui {class FileSetDialog;} class FileSetDialog : public QDialog{ Q_OBJECT public: explicit FileSetDialog(QWidget *parent = nullptr); ~FileSetDialog(); void initTCP(); void newConnect(); private slots: // 与服务器断开连接 void disconnectServer(); // 浏览文件 void selectFile(); // 发送文件 void sendFile(); // 更新文件发送进度 void updateFileProgress(qint64); private: Ui::FileSetDialog *ui; QTcpSocket *tcpSocket; QTcpSocket *fileSocket; // 文件传送 QFile *localFile; // 文件大小 qint64 totalBytes; //文件总字节数 qint64 bytesWritten; //已发送的字节数 qint64 bytestoWrite; //尚未发送的字节数 qint64 filenameSize; //文件名字的字节数 qint64 bytesReceived; //接收的字节数 ///每次发送数据大小 qint64 perDataSize; QString filename; ///数据缓冲区 QByteArray inBlock; QByteArray outBlock; QString selectPath ="";}; #endif // FILESETDIALOG_H
服务端:
.cpp:
#include "filemanagment.h"#include <QDataStream>#include <QMessageBox>#include <QString>#include <QByteArray>#include <QApplication> #include <QTextCodec> FileManagment * FileManagment::widget=nullptr;FileManagment::FileManagment(QWidget *parent) : QWidget(parent){ // 文件传送套接字 this->filesocket = new QTcpSocket(this); this->fileserver = new QTcpServer(this); this->fileserver->listen(QHostAddress::Any,8888); connect(this->fileserver,SIGNAL(newConnection()),this,SLOT(acceptFileConnection())); // 文件传送相关变量初始化 bytesReceived = 0; totalBytes = 0; filenameSize = 0;} FileManagment *FileManagment::getFileManagment(){ if(widget==nullptr) { widget = new FileManagment(); } return widget;} FileManagment::~FileManagment(){ close();// delete m_sever;} void FileManagment::acceptFileConnection(){ bytesWritten = 0; //每次发送数据大小为64kb perDataSize = 64*1024; this->filesocket = this->fileserver->nextPendingConnection(); //接受文件 connect(filesocket,SIGNAL(readyRead()),this,SLOT(updateFileProgress())); connect(filesocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(updateFileProgress(qint64))); connect(filesocket,SIGNAL(bytesWritten(qint64)),this,SLOT(displayError(QAbstractSocket::SocketError socketError))); qDebug() << "acceptFileConnection ";} void FileManagment::updateFileProgress(){ QDataStream inFile(this->filesocket); inFile.setVersion(QDataStream::Qt_4_8); //如果接收到的数据小于16个字节,保存到来的文件头结构 if(bytesReceived <= sizeof(qint64)*2) { if((filesocket->bytesAvailable()>=(sizeof(qint64))*2) && (filenameSize==0)) { inFile>>totalBytes>>filenameSize; bytesReceived += sizeof(qint64)*2; } if((filesocket->bytesAvailable()>=filenameSize) && (filenameSize != 0)) { inFile>>filename; bytesReceived += filenameSize; //创建同名文件 QString filePath = QApplication::applicationDirPath() + "/../../S_Video_v3/fileData"; qDebug() << " 文件夹filePath!" << filePath; QDir dir(filePath); // QDir *temp = new QDir; bool exist = dir.exists(filePath); if(exist) qDebug() << "文件夹已经存在!"; else { bool ok = dir.mkdir(filePath); if( ok ) qDebug() << "文件夹不存在,创建成功!"; } filePath = dir.absolutePath() + QString("/%1").arg(filename); localFile = new QFile(filePath); // // 接收的文件放在指定目录下// filename = "ClientData/"+filename;// localFile = new QFile(filename); if(!localFile->open(QFile::WriteOnly)) { qDebug()<<"Server::open file error!"; return; } } else return; } // /如果接收的数据小于总数据,则写入文件 if(bytesReceived < totalBytes) { bytesReceived += filesocket->bytesAvailable(); inBlock = filesocket->readAll(); localFile->write(inBlock); inBlock.resize(0); } // 数据接收完成时 if(bytesReceived == totalBytes) { // this->ui.browser->append("Receive file successfully!"); bytesReceived = 0; totalBytes = 0; filenameSize = 0; localFile->close(); //filesocket->close(); }} void FileManagment::displayError(QAbstractSocket::SocketError socketError){ qDebug()<< "displayError--" << socket->errorString(); socket->close();}
#ifndef FILEMANAGMENT_H#define FILEMANAGMENT_H#include <QString>#include <QDebug>#include <QTcpServer>#include <QTcpSocket>#include <QWidget>#include <QProcess>#include <iostream>#include <QThread>using namespace std; class FileManagment: public QWidget{ Q_OBJECTpublic: explicit FileManagment(QWidget *parent = nullptr); ~FileManagment(); static FileManagment * getFileManagment(); QTcpServer *server; QTcpSocket *socket; QTcpServer *fileserver; QTcpSocket *filesocket; private slots: void acceptFileConnection(); void updateFileProgress(); void displayError(QAbstractSocket::SocketError socketError); private: static FileManagment *widget;// 传送文件相关变量 qint64 totalBytes; qint64 bytesReceived; qint64 bytestoWrite; qint64 bytesWritten; qint64 filenameSize; QString filename; ///每次发送数据大小 qint64 perDataSize; QFile *localFile;// 本地缓冲区 QByteArray inBlock; QByteArray outBlock;}; #endif // FILEMANAGMENT_H