為了方便網(wǎng)絡(luò)編程,Qt 提供了 Network 模塊。該模塊包含了許多類,本文介紹了Qt實(shí)現(xiàn)FTP的上傳和下載,分享給大家
在網(wǎng)站設(shè)計(jì)、做網(wǎng)站中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報(bào)的無錫營銷推廣。創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站建設(shè)十載了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
本來想簡單抄抄書,隨便手寫個(gè)Ftp客戶端的,結(jié)果發(fā)現(xiàn)教材上的是基于Qt4的QFtp類庫,而在Qt5中取消了這一個(gè)類庫(同時(shí)也取消了QHttp等的類),取而代之的是QNetworkAccessManager 這個(gè)類,把這些雜貨全都攬下來了,據(jù)說是因?yàn)橹暗膬蓚€(gè)類有重復(fù)而且效率有問題balabala。于是就百度了一下,發(fā)現(xiàn)百度上要么講的不全,要么就是要去下一個(gè)由熱心網(wǎng)民重新封裝的QFtp類。顯然我并不喜歡無腦復(fù)制粘貼,想好好看下Qt官方提供的東西的用法,深入的理解下Qt網(wǎng)絡(luò)編程,于是就果斷自行g(shù)oogle(話說google真好用),加上查看幫助文檔,終于把一個(gè)簡版的Ftp客戶端大概框架弄清楚了。
不多說,上源碼:
Dialog.pro
#------------------------------------------------- # # Project created by QtCreator 2015-10-29T23:52:56 # #------------------------------------------------- QT += core gui QT += network #這里要添加這個(gè)庫 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#include #include #include #include #include #include #include #include #include #include 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;//這個(gè)是重點(diǎn) protected slots: //處理按鈕的點(diǎn)擊信號 void slotPut(); void slotGet(); //處理網(wǎng)絡(luò)連接的信號 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);//設(shè)置加密顯示 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);//固定大小 //按鈕點(diǎn)擊事件信號槽的連接 connect(PbPut,SIGNAL(clicked()),this,SLOT(slotPut())); connect(PbGet,SIGNAL(clicked()),this,SLOT(slotGet())); } void Dialog::managePut(QNetworkReply * reply){ qDebug()<error();//輸出調(diào)試信息 switch(reply->error()){//判斷連接后的狀態(tài) 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()< error(); QByteArray data; switch(reply->error()){ case QNetworkReply::NoError: data=reply->readAll();//從url中讀取文件內(nèi)容,輸出到data中(也可以再將數(shù)據(jù)寫入到文件中,為了方便,這里就權(quán)且打印一下吧) 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 ; } //重點(diǎn)!將之前的槽清空并重新連接至需要的 manager.disconnect(SIGNAL(finished(QNetworkReply*))); //完全清空某對象連接的槽可以用manager.disconnect(); connect(&manager,SIGNAL(finished(QNetworkReply*)),SLOT(managePut(QNetworkReply*))); //設(shè)置登錄信息 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將觸發(fā)網(wǎng)絡(luò)的連接信號 } 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" #includeint main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
5、運(yùn)行截圖
權(quán)且只顯示主界面:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。