亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于QT制作一個簡易的傳輸文件小工具

 更新時間:2021年12月18日 09:36:30   作者:一個眉頭緊鎖的碼農(nóng)  
本文主要介紹了通過QT實(shí)現(xiàn)的一個文件傳輸小工具。功能就是能實(shí)現(xiàn)文件的雙向傳輸,即客戶端能傳給服務(wù)端,服務(wù)端可以傳給客戶端。文中示例代碼具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下

最近因?yàn)橐粋€事情很惱火,因?yàn)檗k公需要用到企業(yè)微信,但是企業(yè)微信只能在一個電腦上登陸,所以當(dāng)別人發(fā)文件給你的時候,你只能一個電腦接收,創(chuàng)建共享文件夾也很麻煩,每次都需要去訪問,很麻煩。所以準(zhǔn)備自己寫一個文件傳輸小工具。

功能就是能實(shí)現(xiàn)文件的雙向傳輸,即客戶端能傳給服務(wù)端,服務(wù)端可以傳給客戶端。

使用的tcp通信,其實(shí)就是發(fā)消息,但是組合數(shù)據(jù)我是借鑒了IT1995大神寫的代碼。

先看下效果圖

可以看到既可以接受文件也可進(jìn)行發(fā)送文件,只要2臺電腦在統(tǒng)一局域網(wǎng)內(nèi),就可發(fā)送和接受數(shù)據(jù)。

本地文件下出現(xiàn)了一份傳輸?shù)奈募?/p>

直接看代碼

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QTcpSocket>
#include <QTcpServer>
#include <QFile>
#include <QTextEdit>
#include <QProgressBar>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void Init();

private slots:
    void onTcpConnected();
    void onConnectClicked();
    void ServerNewConnect();
    void SocketReadData();
    void onOpenFileClicked();
    void onSendClicked();
    void updateClientProgress(qint64 numBytes);

private:
    QPushButton *m_pConnectBtn=nullptr;
    QLineEdit *m_pIpAddressEdit=nullptr;
    QLineEdit *m_pPortEdit=nullptr;
    QWidget *m_pTitleWgt=nullptr;

    QLineEdit *m_pFilePathEdit=nullptr;
    QPushButton *m_pOpenFileBtn=nullptr;
    QPushButton *m_pSendBtn=nullptr;

    QTextEdit *m_pTextEdit=nullptr;

    QProgressBar *m_pReceiverBar=nullptr;
    QProgressBar *m_pSendBar=nullptr;


    QTcpSocket *m_pTcpSocket=nullptr;
    QTcpServer *m_pTcpServer=nullptr;
    QTcpSocket *m_pTcpServerSocket=nullptr;
    //------receiver
    qint64 m_bytesReceived;
    qint64 m_fileNameSize;
    qint64 m_totalBytes;
    QString m_fileName;
    QFile *m_localFile;
    QByteArray m_inBlock;

    //send
    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);  //若連接成功,則觸發(fā)此信號
    connect(m_pTcpSocket,SIGNAL(bytesWritten(qint64)),this,SLOT(updateClientProgress(qint64))); //發(fā)送數(shù)據(jù)

    m_pTcpServer=new QTcpServer(this);
    m_totalBytes=0;
    m_bytesReceived=0;
    m_fileNameSize=0;
    connect(m_pTcpServer,&QTcpServer::newConnection,this,&Widget::ServerNewConnect);
     if(!m_pTcpServer->listen(QHostAddress::Any, 2021))   //端口為2021
     {
         QMessageBox::warning(this,"Warning",m_pTcpServer->errorString(),QMessageBox::Ok);
         return;
     }
}

Widget::~Widget()
{
}

void Widget::Init()
{
    m_pConnectBtn=new QPushButton(tr("Connect"),this);
    m_pIpAddressEdit=new QLineEdit(this);
    m_pPortEdit=new QLineEdit(this);
    m_pPortEdit->setValidator(new QIntValidator());
    m_pTitleWgt=new QWidget(this);
    m_pIpAddressEdit->setFixedWidth(200);
    m_pPortEdit->setFixedWidth(200);
    m_pConnectBtn->setFixedSize(100,25);
    QLabel *ipLabel=new QLabel(tr("IpAddress:"),this);
    QLabel *portLabel=new QLabel(tr("Port:"),this);
    ipLabel->setFixedWidth(60);
    portLabel->setFixedWidth(40);
    QHBoxLayout *titleLayout=new QHBoxLayout(this);
    titleLayout->addWidget(ipLabel);
    titleLayout->addWidget(m_pIpAddressEdit);
    titleLayout->addWidget(portLabel);
    titleLayout->addWidget(m_pPortEdit);
    titleLayout->addWidget(m_pConnectBtn);
    titleLayout->setMargin(5);
    titleLayout->setSpacing(10);
    titleLayout->addStretch();
    m_pTitleWgt->setFixedHeight(40);
    m_pTitleWgt->setLayout(titleLayout);

    m_pIpAddressEdit->setText("192.168.2.110");
    m_pPortEdit->setText("2021");

    m_pPortEdit->setEnabled(false);

    m_pFilePathEdit=new QLineEdit(this);
    m_pOpenFileBtn=new QPushButton(tr("Open File"),this);
    m_pSendBtn=new QPushButton(tr("Send"));

    m_pFilePathEdit->setFixedWidth(500);
    m_pOpenFileBtn->setFixedSize(100,25);
    m_pSendBtn->setFixedSize(100,25);

    m_pSendBtn->setEnabled(false);

    QWidget *bottomWgt=new QWidget(this);
    QHBoxLayout *bottomLayout=new QHBoxLayout(this);
    bottomLayout->addWidget(m_pFilePathEdit);
    bottomLayout->addWidget(m_pOpenFileBtn);
    bottomLayout->addWidget(m_pSendBtn);
    bottomLayout->setMargin(5);
    bottomLayout->setSpacing(5);
    bottomLayout->addStretch();
    bottomWgt->setLayout(bottomLayout);

    m_pTextEdit=new QTextEdit(this);

    QLabel *receiverLabel=new QLabel(tr("Receiver Speed"),this);
    QLabel *SendLabel=new QLabel(tr("Send Speed"),this);
    receiverLabel->setFixedWidth(100);
    SendLabel->setFixedWidth(100);
    m_pReceiverBar=new QProgressBar(this);
    m_pSendBar=new QProgressBar(this);
    m_pReceiverBar->setFixedSize(300,30);
    m_pSendBar->setFixedSize(300,30);
    m_pReceiverBar->setOrientation(Qt::Horizontal);
    m_pSendBar->setOrientation(Qt::Horizontal);

    QWidget *receiverBarWgt=new QWidget(this);
    QHBoxLayout *receiverBarLayout=new QHBoxLayout(this);
    receiverBarLayout->addWidget(receiverLabel);
    receiverBarLayout->addWidget(m_pReceiverBar);
    receiverBarLayout->addStretch();
    receiverBarLayout->setSpacing(5);
    receiverBarWgt->setLayout(receiverBarLayout);

    QWidget *sendBarWgt=new QWidget(this);
    QHBoxLayout *sendBarLayout=new QHBoxLayout(this);
    sendBarLayout->addWidget(SendLabel);
    sendBarLayout->addWidget(m_pSendBar);
    sendBarLayout->addStretch();
    sendBarLayout->setSpacing(5);
    sendBarWgt->setLayout(sendBarLayout);

    connect(m_pConnectBtn,&QPushButton::clicked,this,&Widget::onConnectClicked);
    connect(m_pOpenFileBtn,&QPushButton::clicked,this,&Widget::onOpenFileClicked);
    connect(m_pSendBtn,&QPushButton::clicked,this,&Widget::onSendClicked);

    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(m_pTitleWgt);
    mainLayout->addWidget(bottomWgt);
    mainLayout->addWidget(receiverBarWgt);
    mainLayout->addWidget(sendBarWgt);
    mainLayout->addWidget(m_pTextEdit);
    mainLayout->setMargin(0);
    mainLayout->addStretch();
    this->setLayout(mainLayout);

}

void Widget::onTcpConnected()
{
    m_pTextEdit->append("Connect Server Success!");
}

void Widget::onConnectClicked()
{
    QString strip=m_pIpAddressEdit->text();
    QString strport=m_pPortEdit->text();
    if(strip!=""&&strport!="")
    {
        m_pTcpSocket->connectToHost(strip,strport.toInt());  //請求連接
    }
    else
    {
        QMessageBox::warning(this,"Warning","IpAddress or Port is Null",QMessageBox::Ok);
    }
}

void Widget::ServerNewConnect()
{
    m_pTcpServerSocket = m_pTcpServer->nextPendingConnection(); //服務(wù)端接受消息
    QObject::connect(m_pTcpServerSocket, &QTcpSocket::readyRead, this, &Widget::SocketReadData);
    m_pTextEdit->append("Connect Client Success");

}

void Widget::SocketReadData()
{
    QDataStream in(m_pTcpServerSocket);
    in.setVersion(QDataStream::Qt_5_11);
    if (m_bytesReceived<=sizeof(qint64)*2){
        if((m_pTcpServerSocket->bytesAvailable()>=sizeof(qint64)*2)&&(m_fileNameSize==0)){
            in>>m_totalBytes>>m_fileNameSize;
            m_bytesReceived +=sizeof(qint64)*2;
        }

        if((m_pTcpServerSocket->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() << "server: open file error!";
                        return;
                    }
                }
                else{
                    return;
                }
    }

    if(m_bytesReceived<m_totalBytes) {
            m_bytesReceived+=m_pTcpServerSocket->bytesAvailable();
            m_inBlock = m_pTcpServerSocket->readAll();
            m_localFile->write(m_inBlock);
            m_inBlock.resize(0);
        }


        m_pReceiverBar->setMaximum(m_totalBytes);
        m_pReceiverBar->setValue(m_bytesReceived);

        if (m_bytesReceived==m_totalBytes){
            m_localFile->close();
            QString strSuccess=QString("File %1 ReceiverSucess").arg(m_fileName);
            m_pTextEdit->append(strSuccess);
            m_pTcpServerSocket->close();
            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_pSendBtn->setEnabled(true);
        m_pFilePathEdit->setText(fileName);
    }
}

void Widget::onSendClicked()
{
    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 sendOut(&m_ClientoutBlock,QIODevice::WriteOnly);
   sendOut.setVersion(QDataStream::Qt_5_11);
   QString currentFileName=m_ClientfileName.right(m_ClientfileName.size()-m_ClientfileName.lastIndexOf('/')-1);
   sendOut<<qint64(0)<<qint64(0)<<currentFileName;
   m_ClienttotalBytes+=m_ClientoutBlock.size();
   sendOut.device()->seek(0);
   sendOut<<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_ClientbytesToWrite>0){
        m_ClientoutBlock=m_ClientlocalFile->read(qMin(m_ClientbytesToWrite,m_ClinetpayloadSize));
        m_ClientbytesToWrite-=(int)m_pTcpSocket->write(m_ClientoutBlock);
        m_ClientoutBlock.resize(0);
    }
    else{
        m_ClientlocalFile->close();
    }

    m_pSendBar->setMaximum(m_ClienttotalBytes);
    m_pSendBar->setValue(m_ClientbytesWritten);

    if(m_ClientbytesWritten==m_ClienttotalBytes){
        QString sendSuccess=QString("Send File %1 Success").arg(m_fileName);
        m_pTextEdit->append(sendSuccess);
        m_ClientlocalFile->close();
        m_pTcpSocket->close();
        m_ClientbytesWritten=0;
    }
}


這個小工具我會一直用的?

到此這篇關(guān)于基于QT制作一個簡易的傳輸文件小工具的文章就介紹到這了,更多相關(guān)QT傳輸文件工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++利用PCL點(diǎn)云庫操作txt文件詳解

    C++利用PCL點(diǎn)云庫操作txt文件詳解

    這篇文章主要為大家詳細(xì)介紹了C++如何利用PCL點(diǎn)云庫操作txt文件,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2024-01-01
  • 使用OpenGL創(chuàng)建窗口的示例詳解

    使用OpenGL創(chuàng)建窗口的示例詳解

    OpenGL,也就是Open?Graphics?Library。其主要就是用于我們?nèi)ヤ秩?D、3D矢量圖形的一種跨語言、跨平臺的應(yīng)用程序編程接口,這篇文章主要介紹了使用OpenGL創(chuàng)建窗口,需要的朋友可以參考下
    2022-04-04
  • C++之vector內(nèi)存釋放原理

    C++之vector內(nèi)存釋放原理

    這篇文章主要介紹了C++之vector內(nèi)存釋放原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解C++基礎(chǔ)——類繼承中方法重載

    詳解C++基礎(chǔ)——類繼承中方法重載

    這篇文章主要介紹了C++基礎(chǔ)——類繼承中方法重載,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C++ LeetCode543題解二叉樹直徑

    C++ LeetCode543題解二叉樹直徑

    這篇文章主要為大家介紹了C++ LeetCode543題解二叉樹直徑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • C語言大廠面試技巧及strcpy()函數(shù)示例詳解

    C語言大廠面試技巧及strcpy()函數(shù)示例詳解

    這篇文章主要為大家介紹了C語言面試技巧,以strcpy()函數(shù)為示例進(jìn)行分析詳解,有需要沖刺大廠的朋友們可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • C++中操作符的前置與后置有什么區(qū)別

    C++中操作符的前置與后置有什么區(qū)別

    C 語言提供了豐富的操作符,有:算術(shù)操作符,移位操作符,位操作符,賦值操作符,單目操作符,關(guān)系操作符,邏輯操作符,條件操作符等。接下了讓我們詳細(xì)了解掌握它
    2022-05-05
  • C語言利用system調(diào)用系統(tǒng)命令行詳情

    C語言利用system調(diào)用系統(tǒng)命令行詳情

    這篇文章主要介紹了C語言利用system調(diào)用系統(tǒng)命令行詳情,system就是調(diào)用系統(tǒng)命令行,輸入為字符串,然后把這個字符串輸出給命令行,讓命令行執(zhí)行。下文的具體內(nèi)容,需要的小伙伴可以參考一下
    2022-01-01
  • C語言中條件編譯詳解

    C語言中條件編譯詳解

    預(yù)處理程序提供了條件編譯的功能。可以按不同的條件去編譯不同的程序部分,因而產(chǎn)生不同的目標(biāo)代碼文件。這對于程序的移植和調(diào)試是很有用的。條件編譯有三種形式,下面分別介紹。
    2017-05-05
  • Qt實(shí)現(xiàn)帶字?jǐn)?shù)限制的文字輸入框

    Qt實(shí)現(xiàn)帶字?jǐn)?shù)限制的文字輸入框

    這篇文章介紹了Qt實(shí)現(xiàn)帶字?jǐn)?shù)限制文字輸入框的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評論