欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

Qt實(shí)現(xiàn)FTP的上傳和下載的實(shí)例代碼

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

為了方便網(wǎng)絡(luò)編程,Qt 提供了 Network 模塊。該模塊包含了許多類(lèi),本文介紹了Qt實(shí)現(xiàn)FTP的上傳和下載,分享給大家

本來(lái)想簡(jiǎn)單抄抄書(shū),隨便手寫(xiě)個(gè)Ftp客戶(hù)端的,結(jié)果發(fā)現(xiàn)教材上的是基于Qt4的QFtp類(lèi)庫(kù),而在Qt5中取消了這一個(gè)類(lèi)庫(kù)(同時(shí)也取消了QHttp等的類(lèi)),取而代之的是QNetworkAccessManager 這個(gè)類(lèi),把這些雜貨全都攬下來(lái)了,據(jù)說(shuō)是因?yàn)橹暗膬蓚€(gè)類(lèi)有重復(fù)而且效率有問(wèn)題balabala。于是就百度了一下,發(fā)現(xiàn)百度上要么講的不全,要么就是要去下一個(gè)由熱心網(wǎng)民重新封裝的QFtp類(lèi)。顯然我并不喜歡無(wú)腦復(fù)制粘貼,想好好看下Qt官方提供的東西的用法,深入的理解下Qt網(wǎng)絡(luò)編程,于是就果斷自行g(shù)oogle(話說(shuō)google真好用),加上查看幫助文檔,終于把一個(gè)簡(jiǎn)版的Ftp客戶(hù)端大概框架弄清楚了。

不多說(shuō),上源碼:

Dialog.pro 

#-------------------------------------------------
#
# Project created by QtCreator 2015-10-29T23:52:56
#
#-------------------------------------------------
QT += core gui
QT += network #這里要添加這個(gè)庫(kù)
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;//這個(gè)是重點(diǎn)
protected slots:
 //處理按鈕的點(diǎn)擊信號(hào)
 void slotPut();
 void slotGet();
 //處理網(wǎng)絡(luò)連接的信號(hào)
 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)擊事件信號(hào)槽的連接
 connect(PbPut,SIGNAL(clicked()),this,SLOT(slotPut()));
 connect(PbGet,SIGNAL(clicked()),this,SLOT(slotGet()));
}
void Dialog::managePut(QNetworkReply * reply){
 qDebug()<<reply->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類(lèi)似 
 qDebug()<<reply->error();
 QByteArray data;
 switch(reply->error()){
 case QNetworkReply::NoError:
 data=reply->readAll();//從url中讀取文件內(nèi)容,輸出到data中(也可以再將數(shù)據(jù)寫(xiě)入到文件中,為了方便,這里就權(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*)));
 //完全清空某對(duì)象連接的槽可以用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ò)的連接信號(hào)
}
void Dialog::slotGet(){
 //基本意義與slotPut類(lèi)似
 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、運(yùn)行截圖

權(quán)且只顯示主界面:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有