QT5下编写ftp客户端下载工具,可直接使用ftp接口,不用再次编译QFtp

标签:
qt5ftp嵌入式c跨平台 |
分类: QT4/5 |
源码下载地址:https://download.csdn.net/download/wandaozhong/10577089
废话不多说,先上图看效果:
部分代码展示:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
file = new QFile(this);
ftp = new QFtp(this);
// 当每条命令开始执行时发出相应的信号
connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpCommandStarted(int)));
// 当每条命令执行结束时发出相应的信号
connect(ftp,SIGNAL(commandFinished(int,bool)),SLOT(ftpCommandFinished(int,bool)));
ui->progressBar->setValue(0);
//鼠标双击列表中的目录时,我们进入该目录
connect(ui->fileList,SIGNAL(itemActivated(QTreeWidgetItem*,int)),this,SLOT(processItem(QTreeWidgetItem*,int)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::processItem(QTreeWidgetItem* item,int) //双击打开一个目录
{
QString name = item->text(0);
if (isDirectory.value(name)) { //如果这个文件是个目录,则打开
ui->fileList->clear();
isDirectory.clear();
currentPath += '/';
currentPath += name;
ftp->cd(name);
ftp->list();
ui->cdToParentButton->setEnabled(true);
}
}
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("连接到服务器成功"));
}
if (ftp->currentCommand() == QFtp::Login){
if(error)
ui->label->setText(tr("登录出现错误:%1")
.arg(ftp->errorString()));
else
{
ui->label->setText(tr("登录成功"));
ftp->list(); //发射listInfo()信号,显示文件列表
}
}
if (ftp->currentCommand() == QFtp::Get){
if(error)
ui->label->setText(tr("下载出现错误:%1")
.arg(ftp->errorString()));
else {
ui->label->setText(tr("已经完成下载"));
ui->downloaon->setEnabled(true);
file->close();
delete file;
}
}
else if (ftp->currentCommand() == QFtp::Close){
ui->label->setText(tr("已经关闭连接"));
}
if (ftp->currentCommand() == QFtp::List){
if (isDirectory.isEmpty())
{ //如果目录为空,显示“empty”
ui->fileList->addTopLevelItem(
new QTreeWidgetItem(QStringList()<< tr("")));
ui->fileList->setEnabled(false);
ui->label->setText(tr("该目录为空"));
}
}
}
void Widget::on_connectButton_clicked()
{
ui->fileList->clear();
currentPath.clear();
isDirectory.clear();
ftp = new QFtp(this);
connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftpCommandStarted(int)));
connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(ftpCommandFinished(int,bool)));
connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(addToList(QUrlInfo)));
connect(ftp,SIGNAL(dataTransferProgress(qint64,qint64)),this,SLOT(updateDataTransferProgress(qint64,qint64)));
QString ftpServer = ui->ftpServerLineEdit->text();
QString userName = ui->userNameLineEdit->text();
QString passWord = ui->passWordLineEdit->text();
ftp->connectToHost(ftpServer,21); //连接到服务器,默认端口号是21
ftp->login(userName,passWord); //登录
}
void Widget::updateDataTransferProgress (qint64 readBytes,qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(readBytes);
}
void Widget::addToList(const QUrlInfo &urlInfo)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
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("MMM dd yyyy"));
QPixmap pixmap(urlInfo.isDir() ? "../ftpclient2/dir.png" : "../ftpclient2/file.png");
item->setIcon(0, pixmap);
isDirectory[urlInfo.name()] = urlInfo.isDir();
//存储该路径是否为目录的信息
ui->fileList->addTopLevelItem(item);
if (!ui->fileList->currentItem()) {
ui->fileList->setCurrentItem(ui->fileList->topLevelItem(0));
ui->fileList->setEnabled(true);
}
}
void Widget::on_cdToParentButton_clicked()
{
ui->fileList->clear();
isDirectory.clear();
currentPath = currentPath.left(currentPath.lastIndexOf('/'));
if (currentPath.isEmpty()) {
ui->cdToParentButton->setEnabled(false);
ftp->cd("/");
} else {
ftp->cd(currentPath);
}
ftp->list();
}
void Widget::on_downloaon_clicked()
{
QString fileName = ui->fileList->currentItem()->text(0);
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly))
{
delete file;
return;
}
//下载按钮不可用,等下载完成后才可用
ui->downloaon->setEnabled(false);
ftp->get(ui->fileList->currentItem()->text(0), file);
}
仁至义尽啊!!!
新手在此感谢yafeilinux开源社区!!!