您好,登錄后才能下訂單哦!
為了方便網絡編程,Qt 提供了 Network 模塊。該模塊包含了許多類,本文介紹了Qt實現FTP的上傳和下載,分享給大家
本來想簡單抄抄書,隨便手寫個Ftp客戶端的,結果發現教材上的是基于Qt4的QFtp類庫,而在Qt5中取消了這一個類庫(同時也取消了QHttp等的類),取而代之的是QNetworkAccessManager 這個類,把這些雜貨全都攬下來了,據說是因為之前的兩個類有重復而且效率有問題balabala。于是就百度了一下,發現百度上要么講的不全,要么就是要去下一個由熱心網民重新封裝的QFtp類。顯然我并不喜歡無腦復制粘貼,想好好看下Qt官方提供的東西的用法,深入的理解下Qt網絡編程,于是就果斷自行google(話說google真好用),加上查看幫助文檔,終于把一個簡版的Ftp客戶端大概框架弄清楚了。
不多說,上源碼:
Dialog.pro
#------------------------------------------------- # # Project created by QtCreator 2015-10-29T23:52:56 # #------------------------------------------------- QT += core gui QT += network #這里要添加這個庫 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = QFtp TEMPLATE = app SOURCES += main.cpp dialog.cpp HEADERS += dialog.h
dialog.h
#ifndef DIALOG_H #define DIALOG_H //注意需要添加的頭文件 #include<QDialog> #include<QPushButton> #include<QFile> #include<QNetworkReply> #include<QLineEdit> #include<QtNetwork/QNetworkAccessManager> #include<QtNetwork/QNetworkRequest> #include<QLabel> #include<QString> #include<QGridLayout> #include<QMessageBox> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); public: QGridLayout *layout; QLabel *LbServer,*LbUser,*LbPasswd; QLineEdit *LeServer,*LeUser,*LePasswd; QPushButton *PbPut,*PbGet; QNetworkAccessManager manager;//這個是重點 protected slots: //處理按鈕的點擊信號 void slotPut(); void slotGet(); //處理網絡連接的信號 void managePut(QNetworkReply*); void manageGet(QNetworkReply*); }; #endif // DIALOG_H
dialog.cpp
#include "dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle("My Ftp"); layout=new QGridLayout(this); LbServer=new QLabel("Server:"); LbUser=new QLabel("User:"); LbPasswd=new QLabel("Passwd:"); LeServer=new QLineEdit("ftp://120.27.41.126/home/myths/1.txt"); LeUser=new QLineEdit("myths"); LePasswd=new QLineEdit("123456"); LePasswd->setEchoMode(QLineEdit::Password);//設置加密顯示 PbPut=new QPushButton("Put"); PbGet=new QPushButton("Get"); layout->addWidget(LbServer,0,0); layout->addWidget(LeServer,0,1); layout->addWidget(LbUser,1,0); layout->addWidget(LeUser,1,1); layout->addWidget(LbPasswd,2,0); layout->addWidget(LePasswd,2,1); layout->addWidget(PbPut,3,0); layout->addWidget(PbGet,3,1); setFixedSize(300,200);//固定大小 //按鈕點擊事件信號槽的連接 connect(PbPut,SIGNAL(clicked()),this,SLOT(slotPut())); connect(PbGet,SIGNAL(clicked()),this,SLOT(slotGet())); } void Dialog::managePut(QNetworkReply * reply){ qDebug()<<reply->error();//輸出調試信息 switch(reply->error()){//判斷連接后的狀態 case QNetworkReply::NoError: QMessageBox::information(this,"Put information","Upload Success!"); break; case QNetworkReply::HostNotFoundError: QMessageBox::information(this,"Put information","Host Not Found!"); break; case QNetworkReply::AuthenticationRequiredError: QMessageBox::information(this,"Put information","Login Failure!"); break; default: QMessageBox::information(this,"Put information","Unknown Failure"); break; } } void Dialog::manageGet(QNetworkReply *reply){ //基本和managerPut類似 qDebug()<<reply->error(); QByteArray data; switch(reply->error()){ case QNetworkReply::NoError: data=reply->readAll();//從url中讀取文件內容,輸出到data中(也可以再將數據寫入到文件中,為了方便,這里就權且打印一下吧) QMessageBox::information(this,"Put information","Upload Success!nThe file you've got is :n"+data); break; case QNetworkReply::HostNotFoundError: QMessageBox::information(this,"Put information","Host Not Found!"); break; case QNetworkReply::AuthenticationRequiredError: QMessageBox::information(this,"Put information","Login Failure!"); break; default: QMessageBox::information(this,"Put information","Unknown Failure"); break; } } Dialog::~Dialog() { } void Dialog::slotPut(){ //判斷信息輸入完整 if(LeUser->text().isEmpty()||LePasswd->text().isEmpty()||LeServer->text().isEmpty()){ QMessageBox::warning(this,"Error","Please fill in the information"); return ; } //重點!將之前的槽清空并重新連接至需要的 manager.disconnect(SIGNAL(finished(QNetworkReply*))); //完全清空某對象連接的槽可以用manager.disconnect(); connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(managePut(QNetworkReply*))); //設置登錄信息 QUrl url(LeServer->text()); url.setPort(21); url.setUserName(LeUser->text()); url.setPassword(LePasswd->text()); QByteArray data="This is the test data.n"; /*QNetworkReply *reply=*/ manager.put(QNetworkRequest(url),data);//將data上傳到url中,返回的reply將觸發網絡的連接信號 } void Dialog::slotGet(){ //基本意義與slotPut類似 if(LeUser->text().isEmpty()||LePasswd->text().isEmpty()||LeServer->text().isEmpty()){ QMessageBox::warning(this,"Error","Please fill in the information"); return ; } manager.disconnect(SIGNAL(finished(QNetworkReply*))); connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(manageGet(QNetworkReply*))); QUrl url(LeServer->text()); url.setPort(21); url.setUserName(LeUser->text()); url.setPassword(LePasswd->text()); /*QNetworkReply *reply=*/ manager.get((QNetworkRequest(url))); }
main.cpp
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
5、運行截圖
權且只顯示主界面:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。