温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用QFtp对QT5进行编译

发布时间:2021-02-04 15:02:18 来源:亿速云 阅读:174 作者:Leah 栏目:开发技术

本篇文章为大家展示了怎么使用QFtp对QT5进行编译,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

编译

  • 修改 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"。

运行示例项目

  • 作为qftp/qftp.pro 项目的子项目,直接编译examples项目,只需要更改qftp\examples\qftp\ftpwindow.cpp中的43行,将#include <QtFtp>改为#include <QFtp>,即可以编译并运行。

  • 作为独立项目,除了修改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版本对应;编译好了,运行前,请注意目录结构是否正确。

上述内容就是怎么使用QFtp对QT5进行编译,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI