关于c++:QT5编译使用QFtp

3次阅读

共计 2655 个字符,预计需要花费 7 分钟才能阅读完成。

背景

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

下载

从 GitHub 下载 QFtp:

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 新对象进行如下设置,去掉了勾销操作,勾销操作有问题,临时屏蔽。

 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);

屏蔽勾销按钮的音讯链接。

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

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

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 版本对应;编译好了,运行前,请留神目录构造是否正确。

小结

下面是正统办法在 qt5 中应用 qftp,还能够间接把其源代码纳入你的利用我的项目中,因为一共只有四个文件,稍作批改就能够应用。我发现该项目标问题,次要是 cancelDownload 会出让程序解体,感觉问题出在本地文件曾经被革除,还有后续的数据到来,后果就异样了。有工夫再来钻研,看能不能把协定学透,本人造个轮子进去。

正文完
 0