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