16,211
社区成员
发帖
与我相关
我的任务
分享
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ftp = new QFtp(this);
ftp->connectToHost("ftp.qt.nokia.com"); //连接到服务器
ftp->login(); //登录
ftp->cd("qt"); //跳转到“qt”目录下
QFile *file = new QFile("INSTALL");
if (!file->open(QIODevice::WriteOnly))
{
delete file;
return;
}
ftp->get("INSTALL", file);
connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpCommandStarted(int)));
connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(ftpCommandFinished(int,bool)));
ftp->close(); //关闭连接
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Widget::ftpCommandStarted(int)
{
if(ftp->currentCommand() == QFtp::ConnectToHost)
{
ui->label->setText(tr("正在连接到服务器..."));
}
if(ftp->currentCommand() == QFtp::Login)
{
ui->label->setText(tr("正在登录..."));
}
if(ftp->currentCommand() == QFtp::Get)
{
ui->label->setText(tr("正在下载..."));
}
else if(ftp->currentCommand() == QFtp::Close)
{
ui->label->setText(tr("正在关闭连接..."));
}
}
void Widget::ftpCommandFinished(int,bool error)
{
if(ftp->currentCommand() == QFtp::ConnectToHost)
{
if(error)
ui->label->setText(tr("连接服务器出现错误:%1").arg(ftp->errorString()));
else
ui->label->setText(tr("连接到服务器成功"));
return;
}
if(ftp->currentCommand() == QFtp::Login)
{
if(error)
ui->label->setText(tr("登录出现错误:%1").arg(ftp->errorString()));
else
{
ui->label->setText(tr("登录成功"));
}
}
if(ftp->currentCommand() == QFtp::Get)
{
if(error)
ui->label->setText(tr("下载出现错误:%1").arg(ftp->errorString()));
else
{
ui->label->setText(tr("已经完成下载"));
ui->textBrowser->setText(ftp->readAll());
}
}
else if (ftp->currentCommand() == QFtp::Close)
{
ui->label->setText(tr("已经关闭连接"));
}
}