首页 > 作文

基于QT制作一个简易的传输文件小工具

更新时间:2023-04-04 03:49:30 阅读: 评论:0

最近因为一个事情很恼火,因为办公需要用到企业微信,但是企业微信只能在一个电脑上登陆,所以当别人发文件给你的时候,你只能一个电脑接收,创建共享文件夹也很麻烦,每次都需要去访问,很麻烦。所以准备自己写一个文件传输小工具。

功能就是能实现文件的双向传输,即客户端能传给服务端,服务端可以传给客户端。

使用的tcp通信,其实就是发消息,但是组合数据我是借鉴了it1995大神写的代码。

先看下效果图

可以看到既可以接受文件也可进行发送文件,只要2台电脑在统一局域网内,就可发送和接受数据。

本地文件下出现了一份传输的文件。

直接看代码

.h

#ifndef widget_h#define widget_h#include <qwidget>#include <qpushbutton>#include <qlineedit>#include <qtcpsocket>#include <qtcprver>#include <qfile>#include <qtextedit>#include <qprogressbar>class widget : public qwidget{    q_objectpublic:    widget(qwidget *parent = nullptr);    ~widget();    void init();private slots:    void ontcpconnected();    void onconnectclicked();心情不好怎么安慰    void rvernewconnect();    void socketreaddata();    void onopenfileclicked();    void onndclicked();    void updateclientprogress(qint64 numbytes);private:    qpushbutton *m_pconnectbtn=nullptr;    qlineedit *m_pipaddresdit=nullptr;    qlineedit *m_pportedit=nullptr;    qwidget *m_ptitlewgt=nullptr;    qlineedit *m_pfilepathedit=nullptr;    qpushbutton *m_popenfilebtn=nullptr;    qpushbutton *m_pndbtn=nullptr;    qtextedit *m_ptextedit=nullptr;    qprogressbar *m_preceiverbar=nullptr;    qprogressbar *m_pndbar=nullptr;    qtcpsocket *m_ptcpsocket=nullptr;    qtcprver *m_ptcprver=nullptr;    qtcpsocket *m_ptcprversocket=nullptr;    //------receiver    qint64 m_bytesreceived;    qint64 m_filenamesize;    qint64 m_totalbytes;    qstring m_filename;    qfile *m_localfile;    qbytearray m_inblock;    //nd    qfile *m_clientlocalfile;    qstring m_clientfilename;    qint64 m_clienttotalbytes;    qint64 m_clientbyteswritten=0;    qint64 m_clientbytestowrite;    qint64 m_clinetpayloadsize;    qbytearray m_clientoutblock;};#endif // widget_h

.cpp

#include "widget.h"#include <qhboxlayout>#include <qlabel>#include <qvalidator>#include <qmessagebox>#include <qfiledialog>widget::widget(qwidget *parent): qwidget(parent){this->resize(800,600);init();m_ptcpsocket=new qtcpsocket(this);connect(m_ptcpsocket,&qtcpsocket::connected,this,&widget::ontcpconnected);  //若连接成功,则触发此信号connect(m_ptcpsocket,signal(byteswritten(qint64)),this,slot(updateclientprogress(qint64))); //发送数据m_ptcprver=new qtcprver(this);m_totalbytes=0;m_bytesreceived=0;m_filenamesize=0;connect(m_ptcprver,&qtcprver::newconnection,this,&widget::rvernewconnect);if(!m_ptcprver->listen(qhostaddress::any, 2021))   //端口为2021{qmessagebox::warning(this,"warning",m_ptcprver->errorstring(),qmessagebox::ok);return;}}widget::~widget(){}void widget::init(){m_pconnectbtn=new qpushbutton(tr("connect"),this);m_pipaddresdit=new qlineedit(this);m_pportedit=new qlineedit(this);m_pportedit->tvalidator(new qintvalidator());m_ptitlewgt=new qwidget(this);m_pipaddresdit->tfixedwidth(200);m_pportedit->tfixedwidth(200);m_pconnectbtn->tfixedsize(100,25);qlabel *iplabel=new qlabel(tr("ipaddress:"),this);qlabel *portlabel=new qlabel(tr("port:"),this);iplabel->tfixedwidth(60);portlabel->tfixedwidth(40);qhboxlayout *titlelayout=new qhboxlayout(this);titlelayout->addwidget(iplabel);titlelayout->addwidget(m_pipaddresdit);titlelayout->addwidget(portlabel);titlelayout->addwidget(m_pportedit);titlelayout->addwidget(m_pconnectbtn);titlelayout->tmargin(5);titlelayout->tspacing(10);titlelayout->addstretch();m_ptitlewgt->tfixedheight(40);m_ptitlewgt->tlayout(titlelayout);m_pipaddresdit->ttext("192.168.2.110");m_pportedit->ttext("2021");m_pportedit->tenabled(fal);m_pfilepathedit=new qlineedit(this);m_popenfilebtn=new qpushbutton(tr("open file"),this);m_pndbtn=new qpushbutton(tr("nd"));m_pfilepathedit->tfixedwidth(500);m_popenfilebtn->tfixedsize(100,25);m_pndbtn->tfixedsize(100,25);m_pndbtn->tenabled(fal);qwidget *bottomwgt=new qwidget(this);qhboxlayout *bottomlayout=new qhboxlayout(this);bottomlayout->addwidget(m_pfilepathedit);bottomlayout->addwidget(m_popenfilebtn);bottomlayout->addwidget(m_pndbtn);bottomlayout->tmargin(5);bottomlayout->tspacing(5);bottomlayout->addstretch();bottomwgt->tlayout(bottomlayout);m_ptextedit=new qtextedit(this);qlabel *receiverlabel=new qlabel(tr("receiver speed"),this);qlabel *ndlabel=new qlabel(tr("nd speed"),this);receiverlabel->tfixedwidth(100);ndlabel->tfixedwidth(100);m_preceiverbar=new qprogressbar(this);m_pndbar=new qprogressbar(this);m_preceiverbar->tfixedsize(300,30);m_pndbar->tfixedsize(300,30);m_preceiverbar->torientation(qt::horizontal);m_pndbar->torientation(qt::horizontal);qwidget *receiverbarwgt=new qwidget(this);qhboxlayout *receiverbarlayout=new qhboxlayout(this);receiverbarlayout->addwidget(receiverlabel);receiverbarlayout->addwidget(m_preceiverbar);receiverbarlayout->addstretch();receiverbarlayout->tspacing(5);receiverbarwgt->tlayout(receiverbarlayout);qwidget *ndbarwgt=new qwidget(this);qhboxlayout *ndbarlayout=new qhboxlayout(this)好评语30字;ndbarlayout->addwidget(ndlabel);ndbarlayout->addwidget(m_pndbar);ndbarlayout->addstretch();ndbarlayout->tspacing(5);ndbarwgt->tlayout(ndbarlayout);connect(m_pconnectbtn,&qpushbutton::clicked,this,&widget::onconnectclicked);connect(m_popenfilebtn,&qpushbutton::clicked,this,&widget::onopenfileclicked);connect(m_pndbtn,&qpushbutton::clicked,this,&widget::onndclicked);qvboxlayout *mainlayout=new qvboxlayout(this);mainlayout->addwidget(m_ptitlewgt);mainlayout->addwidget(bottomwgt);mainlayout->addwidget(receiverbarwgt);mainlayout->addwidget(ndbarwgt);mainlayout->addwidget(m_ptextedit);mainlayout->tmargin(0);mainlayout->addstretch();this->tlayout(mainlayout);}void widget::ontcpconnected(){m_ptextedit->append("connect rver success!");}void widget::onconnectclicked(){qstring strip=m_pipaddresdit->text();qstring strport=m_pportedit->text();if(strip!=""&&strport!=""){m_ptcpsocket->connecttohost(strip,strport.toint());  //请求连接}el{qmessagebox::warning(this,"warning","ipaddress or port is null",qmessagebox::ok);}}void widget::rvernewconnect(){m_ptcprversocket = m_ptcprver->nextpendingconnection(); //服务端接受消息qobject::connect(m_ptcprversocket, &qtcpsocket::readyread, this, &widget::socketreaddata);m_ptextedit->append("connect client success");动词加ed}void widget::socketreaddata(){qdatastream in(m_ptcprversocket);in.tversion(qdatastream::qt_5_11);if (m_bytesreceived<=sizeof(qint64)*2){if((m_ptcprversocket->bytesavailable()>=sizeof(qint64)*2)&&(m_filenamesize==0)){in>>m_totalbytes>>m_filenamesize;m_bytesreceived +=sizeof(qint64)*2;}if((m_ptcprversocket->bytesavailable()>=m_filenamesize)&&(m_filenamesize!=0)){in>>m_filename;m_bytesreceived+=m_filenamesize;m_localfile = new qfile(m_filename);if (!m_localfile->open(qfile::writeonly)){qdebug() << "rver: open file error!";return;}}el{return;}}if(m_bytesreceived<m_totalbytes) {m_bytesreceived+=m_ptcprversocket->bytesavailable();m_inblock = m_ptcprversocket->readall();m_localfile->write(m_inblock);m_inblock.resize(0);}m_preceiverbar->tmaximum(m_totalbytes);m_preceiverbar->tvalue(m_bytesreceived);if (m_bytesreceived==m_totalbytes){m_localfile->clo();qstring strsuccess=qstring("file %1 receiversucess").arg(m_filename);m_ptextedit->append(strsuccess);m_ptcprversocket->clo();m_totalbytes=0;m_bytesreceived=0;m_filenamesize=0;}}void widget::onopenfileclicked(){qstring filename = qfiledialog::getopenfilename(this, tr("open file"),"/home",tr("file (*.*)"));if(filename!=""){m_clientfilename=filename;m_pndbtn->tenabled(true);m_pfilepathedit->ttext(filename);}}void widget::onndclicked(){m_clientoutblock.clear();m_clientlocalfile=new qfile(m_clientfilename);if(!m_clientlocalfile->open(qfile::readonly)){qdebug()<<"client:open file error!";return;}m_clienttotalbytes=m_clientlocalfile->size();qdatastream ndout(&m_clientoutblock,qiodevice::writeonly);ndout.tversion(qdatastream::qt_5_11);qstring currentfilename=m_clientfilename.right(m_clientfilename.size()-m_clientfilename.lastindexof('/')-1);ndout<<qint64(0)<<qint64(0)<<currentfilename;m_clienttotalbytes+=m_clientoutblock.size();ndout.device()->ek(0);ndout<<m_clienttotalbytes<<qint64(m_clientoutblock.size()-sizeof(qint64)*2);m_clientbytestowrite=m_clienttotalbytes-m_ptcpsocket->write(m_clientoutblock);m_clientoutblock.resize(0);}void widget::updateclientprogress(qint64 numbytes){m_clientbyteswritten+=(int)numbytes;if(m_clientby真情真美testowrite>0){m_clientoutblock=m_clientlocalfile->read(qmin(m_clientbytestowrite,m_clinetpayloadsize));m_clientbytestowrite-=(int)m_ptcpsocket->write(m_clientoutblock);m_clientoutblock.resize(0);}el{m_clientlocalfile->clo();}m_pndbar->tmaximum(m_clienttotalbytes);m_pndbar->tvalue(m_clientbyteswritten);if(m_clientbyteswritten==m_clienttotalbytes){qstring ndsuccess=qstring("nd file %1 success"深化农村改革综合性实施方案).arg(m_filename);m_ptextedit->append(ndsuccess);m_clientlocalfile->clo();m_ptcpsocket->clo();m_clientbyteswritten=0;}}

这个小工具我会一直用的

到此这篇关于基于qt制作一个简易的传输文件小工具的文章就介绍到这了,更多相关qt传输文件工具内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 03:49:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/2258e94fdffaf6636330aae76eeec5a5.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:基于QT制作一个简易的传输文件小工具.doc

本文 PDF 下载地址:基于QT制作一个简易的传输文件小工具.pdf

标签:文件   服务端   小工具   数据
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图