QT5编译使用QFtp

背景

使用 QNetworkAccessManager 能够实现 Ftp 的上传/下载功能,但它没有提供例如list、cd、remove、mkdir、rmdir、rename 等功能。这种状况下,咱们可使用QFtp,须要下载源码、编译并处理一些坑。git

下载

从 GitHub 下载 QFtp:github

https://github.com/qt/qtftp

编译

  • 修改 qftp/qftp.pro,删除最后一行,module_qtftp_tests。否则编译会有错误,这个是测试子项目,暂时去除,先编译使用。
  • 修改 qftp/src/qftp/qftp.h 第47行 #include <QtFtp/qurlinfo.h> => #include <qurlinfo.h>
  • 修改 qftp/src/qftp/qftp.pro 第4,5行的+,-号互换,生成*.dll
  • 修改第4行为 CONFIG += staticlib,生成.lib和.prl

用qtcreator打开qftp/qftp.pro,编译生成库文件。架构

放入QT5安装目录中

我以Qt5.5.1为例说明,其它版本相似函数

  • 将 Qt5Ftpd.lib、Qt5Ftp.lib、Qt5Ftpd.prl、Qt5Ftp.prl 拷贝至 D:\Qt\Qt5.5.1\5.5\msvc2010\lib。
  • 将Qt5Ftpd.dll、Qt5Ftp.dll 拷贝至D:\Qt\Qt5.5.1\5.5\msvc2010\bin。
  • 将qftp.h、qurlinfo.h 拷贝至 D:\Qt\Qt5.5.1\5.5\msvc2010\include\QtNetwork,并新建一个名为 QFtp 的文件(没有后缀名),而后用本写入 #include "qftp.h"

运行示例项目

  1. 做为qftp/qftp.pro 项目的子项目,直接编译examples项目,只须要更改qftp\examples\qftp\ftpwindow.cpp中的43行,将#include <QtFtp>改成#include <QFtp>,便可以编译并运行。
  2. 做为独立项目,除了修改1中的这项,还须要修改qftp\examples\qftp\qftp.pro中ftp库的加载方式,从第五行中删除ftp,而后增长以下代码:
CONFIG(debug, debug|release) {
   LIBS += -lQt5Ftpd
} else {
   LIBS += -lQt5Ftp
}

也就是说,ftp的加载方式还不能与Qt5的原生库彻底一致,如何作到这一点,我还须要时间研究。测试

示例项目改进

修正进度条的提早显示,对progressDialog新对象进行以下设置,去掉了取消操做,取消操做有问题,暂时屏蔽。ui

progressDialog = new QProgressDialog("download...", nullptr, 0, 100, this);
 progressDialog->setWindowModality(Qt::WindowModal);
 auto winFlags = windowFlags() & ~Qt::WindowMinMaxButtonsHint;
 progressDialog->setWindowFlags(winFlags &~ Qt::WindowCloseButtonHint); //去掉窗口的默认按钮
 progressDialog->reset();                   //避免提早显示
 progressDialog->setAutoClose(false);
 progressDialog->setAutoReset(false);

屏蔽取消按钮的消息连接。this

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

支持多文件下载
首先,在QTreeWidget生成后,设置其能够选中多行。url

fileList->setSelectionMode(QAbstractItemView::ExtendedSelection);

修改downloadFile函数,支持多文件下载。命令行

QList<QTreeWidgetItem*> selectedItemList = fileList->selectedItems();
for (int i = 0; i < selectedItemList.size(); i++)
 {
    QString fileName = selectedItemList[i]->text(0);
    if (QFile::exists(fileName)) {
        QMessageBox::information(this, tr("FTP"),
        tr("There already exists a file called %1 in the current directory.").arg(fileName));
        return;
    }
    file = new QFile(fileName);
    if (!file->open(QIODevice::WriteOnly)) {
        QMessageBox::information(this, tr("FTP"),
        tr("Unable to save the file %1: %2.").arg(fileName).arg(file->errorString()));
        delete file;
        return;
    }
    ftp->get(fileName, file);
    progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
    downloadButton->setEnabled(false);
    progressDialog->exec();
}

项目地址

https://github.com/zhoutk/qtDemo

命令行编译

git clone https://github.com/zhoutk/qtDemo
cd qtDemo/qftp & mkdir build & cd build
cmake ..
cmake --build .

编译时注意:cmake默认为x86架构,须要与你安装的Qt版本对应;编译好了,运行前,请注意目录结构是否正确。debug

小结

上面是正统方法在qt5中使用qftp,还能够直接把其源代码归入你的应用项目中,由于一共只有四个文件,稍做修改就可使用。我发现该项目的问题,主要是cancelDownload会出让程序崩溃,感受问题出在本地文件已经被清除,还有后续的数据到来,结果就异常了。有时间再来研究,看能不能把协议学透,本身造个轮子出来。

相关文章
相关标签/搜索