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會出讓程序崩潰,感受問題出在本地文件已經被清除,還有後續的數據到來,結果就異常了。有時間再來研究,看能不能把協議學透,本身造個輪子出來。

相關文章
相關標籤/搜索