QT程序FTP服务器x.docx

上传人:b****4 文档编号:24630062 上传时间:2023-05-29 格式:DOCX 页数:16 大小:18.08KB
下载 相关 举报
QT程序FTP服务器x.docx_第1页
第1页 / 共16页
QT程序FTP服务器x.docx_第2页
第2页 / 共16页
QT程序FTP服务器x.docx_第3页
第3页 / 共16页
QT程序FTP服务器x.docx_第4页
第4页 / 共16页
QT程序FTP服务器x.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

QT程序FTP服务器x.docx

《QT程序FTP服务器x.docx》由会员分享,可在线阅读,更多相关《QT程序FTP服务器x.docx(16页珍藏版)》请在冰豆网上搜索。

QT程序FTP服务器x.docx

QT程序FTP服务器x

QT程序FTP服务器

12级电子新能源本2班

20120553049

张恒

QT简介

QT是一个跨平台的C++图形化用户界面的应用程序开发框架。

它既可以开发GUI程序,也用于开发非GUI程序,比如控制台工具和服务器。

在QTCreator3.1.0正式发布后,QT实现了对IOS、Android、WP的全面支持。

FTP服务器

FTP是文件传输协议的英文简称,用于Internet上的控制文件的双向传输。

同多数Internet服务器一样,FTP也是分为两部分,即客户端程序与服务器系统。

本次所做出的图形化界面程序为FTP的客户端程序。

/

程序介绍

UI介绍

FTP客户端程序的UI界面是图所示,在UI中主要由连接、上传、下载、退出、返回、FTP地址、用户名、密码等组成,并且在下方留出大窗口用于显示FTP服务器中的文件。

程序源代码介绍

#include"ftp.h"

#include"ui_ftp.h"

Ftp:

:

Ftp(QWidget*parent)

:

QWidget(parent),ui(newUi:

:

Ftp),ftpobj(0)

{

ui->setupUi(this);

//ui->lineEdit->setText("");

//ui->comboBox->addItem("192.168.1.253");

ui->lineEdit_3->setEchoMode(QLineEdit:

:

Password);

//ui->treeWidget->setAcceptDrops(true);

this->setAcceptDrops(true);

//model=newQDirModel;

//ui->treeView->setModel(model);

//ui->treeView->setAcceptDrops(true);

ui->label_2->setText(tr("PleaseenterthenameofanFTPserver."));

ui->lineEdit_2->setText("");

ui->lineEdit_3->setText("");

ui->treeWidget->setEnabled(false);

ui->treeWidget->setRootIsDecorated(false);

//ui->treeWidget->setHeaderLabels(QStringList()<

ui->treeWidget->header()->setStretchLastSection(false);

ui->pushButton_2->setDefault(true);

ui->pushButton->setEnabled(false);

ui->radioButton_2->setChecked(true);

ui->pushButton_3->setEnabled(false);

progressDialog=newQProgressDialog(this);

connect(ui->checkBox,SIGNAL(clicked()),this,SLOT(enableCheckBox()));

connect(ui->treeWidget,SIGNAL(itemActivated(QTreeWidgetItem*,int)),

this,SLOT(processItem(QTreeWidgetItem*,int)));

connect(ui->treeWidget,SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),

this,SLOT(enableDownloadButton()));

connect(progressDialog,SIGNAL(canceled()),this,SLOT(cancelDownload()));

connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(connectOrDisconnect()));

connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(cdToParent()));

connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(downloadFile()));

connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(close()));

connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(uploadFile()));

//红色代码用于设置信号槽,定义了PushButton、PushButton_2、PushButton_3等按钮的使用

setWindowTitle(tr("FTP"));

}

Ftp:

:

~Ftp()

{

deleteui;

}

QSizeFtp:

:

sizeHint()const

{

returnQSize(500,300);

}

//!

[0]

voidFtp:

:

connectOrDisconnect()

{

if(!

ui->checkBox->isChecked()){

if(ui->lineEdit_2->text().isEmpty()||ui->lineEdit_3->text().isEmpty())

{

QMessageBox:

:

information(this,tr("FTP"),tr("请检查ftp账号信息,用户名和密码都不允许为空。

"));

return;

}

}

//上述代码用来判断用户输入的用户名与密码是否可以登录ftp服务器

if(ftpobj){

ftpobj->abort();

ftpobj->deleteLater();

ftpobj=0;

//!

[0]

ui->lineEdit_2->setEnabled(true);

ui->lineEdit_3->setEnabled(true);

ui->checkBox->setEnabled(true);

ui->treeWidget->setEnabled(false);

ui->pushButton->setEnabled(false);

ui->pushButton_3->setEnabled(false);

ui->pushButton_2->setEnabled(true);

ui->pushButton_2->setText(tr("Connect"));

ui->comboBox->setEditable(true);

ui->checkBox->setChecked(false);

#ifndefQT_NO_CURSOR

setCursor(Qt:

:

ArrowCursor);

#endif

return;

}

#ifndefQT_NO_CURSOR

setCursor(Qt:

:

WaitCursor);

#endif

ui->comboBox->setEditable(false);

ui->lineEdit_2->setEnabled(false);

ui->lineEdit_3->setEnabled(false);

ui->checkBox->setEnabled(false);

ui->treeWidget->setEnabled(true);

ui->pushButton_2->setEnabled(false);

//!

[1]

ftpobj=newQFtp(this);

connect(ftpobj,SIGNAL(commandFinished(int,bool)),

this,SLOT(ftpCommandFinished(int,bool)));

connect(ftpobj,SIGNAL(listInfo(constQUrlInfo&)),

this,SLOT(addToList(constQUrlInfo&)));

connect(ftpobj,SIGNAL(dataTransferProgress(qint64,qint64)),

this,SLOT(updateDataTransferProgress(qint64,qint64)));

ui->treeWidget->clear();

currentPath.clear();

isDirectory.clear();

//!

[1]

//!

[2]

QUrlurl(ui->comboBox->currentText());

if(!

url.isValid()||url.scheme().toLower()!

=QLatin1String("ftp")){

ftpobj->connectToHost(ui->comboBox->currentText(),21);

if(ui->checkBox->isChecked())

ftpobj->login();

else

ftpobj->login(ui->lineEdit_2->text(),ui->lineEdit_3->text());

}else{

ftpobj->connectToHost(url.host(),url.port(21));

/*if(!

url.userName().isEmpty())

//ftpobj->login(QUrl:

:

fromPercentEncoding(url.userName().toLatin1()),url.password());

ftpobj->login("company","123456");

else

ftpobj->login("company","123456");*/

if(ui->checkBox->isChecked())

ftpobj->login();

else

ftpobj->login(ui->lineEdit_2->text(),ui->lineEdit_3->text());

if(!

url.path().isEmpty())

ftpobj->cd(url.path());

}

if(ui->radioButton_2->isChecked())

ftpobj->setTransferMode(QFtp:

:

Active);

else

ftpobj->setTransferMode(QFtp:

:

Passive);

//!

[2]

ui->label_2->setText(tr("ConnectingtoFTPserver%1...")

.arg(ui->comboBox->currentText()));

}

//!

[3]

voidFtp:

:

downloadFile()

{

QStringfileNam=ui->treeWidget->currentItem()->text(0);

//!

[3]

//

QStringfileName=QFileDialog:

:

getExistingDirectory(this,tr("保存到。

"),

".",

QFileDialog:

:

ShowDirsOnly

|QFileDialog:

:

DontResolveSymlinks);

if(fileName.isNull())return;

fileName+="/";

fileName+=fileNam;

if(QFile:

:

exists(fileName)){

intanswer=QMessageBox:

:

question(this,tr("FTP"),

tr("同名文件%1已经存在,需要覆盖吗?

").arg(fileName),

QMessageBox:

:

Yes|QMessageBox:

:

No);

if(answer==QMessageBox:

:

Yes){

QDirdir(fileName);

dir.rmdir(fileName);

}

if(answer==QMessageBox:

:

No)return;

}

//!

[4]

file=newQFile(fileName);

if(!

file->open(QIODevice:

:

WriteOnly)){

QMessageBox:

:

information(this,tr("FTP"),

tr("Unabletosavethefile%1:

%2.")

.arg(fileName).arg(file->errorString()));

deletefile;

return;

}

ftpobj->get(ui->treeWidget->currentItem()->text(0),file);

progressDialog->setLabelText(tr("Downloading%1...").arg(fileName));

ui->pushButton_3->setEnabled(false);

progressDialog->exec();

}

//!

[4]

voidFtp:

:

uploadFile()

{

//QStringDirName=ui->treeWidget->currentItem()->text(0);

//!

[3]

//

QStringfileName=QFileDialog:

:

getOpenFileName(this,tr("打开文件。

"),

".",

"*");

if(fileName.isNull())return;

if(!

QFile:

:

exists(fileName)){

return;

}

//!

[4]

file_upload=newQFile(fileName);

if(!

file_upload->open(QIODevice:

:

ReadOnly)){

QMessageBox:

:

information(this,tr("FTP"),

tr("Unabletoopenthefile%1:

%2.")

.arg(fileName).arg(file_upload->errorString()));

//deletefile_upload;

return;

}

//ftpobj->get(ui->treeWidget->currentItem()->text(0),file);

ftpobj->put(file_upload,fileName);

progressDialog->setLabelText(tr("Uploading%1...").arg(fileName));

progressDialog->exec();

}

//!

[5]

voidFtp:

:

cancelDownload()

{

ftpobj->abort();

}

//!

[5]

//!

[6]

voidFtp:

:

ftpCommandFinished(int,boolerror)

{

#ifndefQT_NO_CURSOR

setCursor(Qt:

:

ArrowCursor);

#endif

if(ftpobj->currentCommand()==QFtp:

:

ConnectToHost){

if(error){

QMessageBox:

:

information(this,tr("FTP"),

tr("UnabletoconnecttotheFTPserver"

"at%1.Pleasecheckthatthehost"

"nameiscorrect.")

.arg(ui->comboBox->currentText()));

connectOrDisconnect();

return;

}

ui->label_2->setText(tr("Loggedonto%1.")

.arg(ui->comboBox->currentText()));

ui->treeWidget->setFocus();

ui->pushButton_3->setDefault(true);

ui->pushButton_2->setEnabled(true);

return;

}

//!

[6]

//!

[7]

if(ftpobj->currentCommand()==QFtp:

:

Login)

ftpobj->list();

//!

[7]

//!

[8]

if(ftpobj->currentCommand()==QFtp:

:

Get){

if(error){

ui->label_2->setText(tr("Canceleddownloadof%1.")

.arg(file->fileName()));

file->close();

file->remove();

}else{

ui->label_2->setText(tr("Downloaded%1tocurrentdirectory.")

.arg(file->fileName()));

file->close();

}

deletefile;

enableDownloadButton();

progressDialog->hide();

//!

[8]

//!

[9]

}elseif(ftpobj->currentCommand()==QFtp:

:

List){

if(isDirectory.isEmpty()){

ui->treeWidget->addTopLevelItem(newQTreeWidgetItem(QStringList()<")));

ui->treeWidget->setEnabled(false);

}

}

//!

[9]

}

//!

[10]

voidFtp:

:

addToList(constQUrlInfo&urlInfo)

{

QTreeWidgetItem*item=newQTreeWidgetItem;

item->setText(0,urlInfo.name());

item->setText(1,QString:

:

number(urlInfo.size()));

item->setText(2,urlInfo.owner());

item->setText(3,urlInfo.group());

item->setText(4,urlInfo.lastModified().toString("MMMddyyyy"));

QPixmappixmap(urlInfo.isDir()?

":

/images/dir.png":

":

/images/file.png");

item->setIcon(0,pixmap);

isDirectory[urlInfo.name()]=urlInfo.isDir();

ui->treeWidget->addTopLevelItem(item);

if(!

ui->treeWidget->currentItem()){

ui->treeWidget->setCurrentItem(ui->treeWidget->topLevelItem(0));

ui->treeWidget->setEnabled(true);

}

}

//!

[10]

//!

[11]

voidFtp:

:

processItem(QTreeWidgetItem*item,int/*column*/)

{

QStringname=item->text(0);

if(isDirectory.value(name)){

ui->treeWidget->clear();

isDirectory.clear();

currentPath+="/"+name;

ftpobj->cd(name);

ftpobj->list();

ui->pushButton->setEnabled(true);

#ifndefQT_NO_CURSOR

setCursor(Qt:

:

WaitCursor);

#endif

return;

}

}

//!

[11]

//!

[12]

voidFtp:

:

cdToParent()

{

#ifndefQT_NO_CURSOR

setCursor(Qt:

:

WaitCursor);

#endif

ui->treeWidget->clear();

isDirectory.clear();

currentPath=currentPath.left(currentPath.lastIndexOf('/'));

if(currentPath.isEmpty()){

ui->pushButton->setEnabled(false);

ftpobj->cd("/");

}else{

ftpobj->cd(currentPath);

}

ftpobj->list();

}

//!

[12]

//!

[13]

voidFtp:

:

updateDataTransferProgress(qint64readBytes,

qint64totalBytes)

{

progressDialog->setMaximum(totalBytes);

progressDialog->setValue(readBytes);

}

//!

[13]

//!

[14]

voidFtp:

:

enableDownloadButton()

{

QTreeWidgetItem*current=ui->treeWidget->currentItem();

if(current){

QStringcurrentFile=current->text(0);

ui->pushButton_3->setEnabled(!

isDirectory.value(currentFile));

}else{

u

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1