QT5使用QFtp的詳細(xì)步驟
更新時(shí)間:2026年01月05日 09:03:11 作者:273992029
本文主要介紹了QT5使用QFtp的詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
1、QFtp編譯
1.1 下載
下載QFtp源碼,https://github.com/qt/qtftp
git clone https://github.com/qt/qtftp.git
1.2 修改
打開qt工程,修改qftp.pro文件中框選的部分,修改為下圖所示。修改qftp.h文件的qurlinfo.h頭文件,改為下圖,該頭文件路徑有問題
CONFIG += static CONFIG += shared


1.3 編譯
只構(gòu)建src

1.4 部署

- 創(chuàng)建一個(gè)文件夾“qtftp”
- 復(fù)制“ bin、lib、include”文件夾至“qtftp”文件夾中
- 把源碼 src中的頭文件 復(fù)制到 include 文件夾中
lib
include

2.5 使用
在.pro中添加組件,然后程序包含頭文件就行。
# QT += ftp
LIBS += $$XLDFLAGS\
-L/usr/qtftp/lib -lQt5Ftp
INCLUDEPATH += /usr/qtftp/include
#include <QFtp>
客戶端
#ifndef FTPMANAGER_H
#define FTPMANAGER_H
#include <QObject>
//#include <QFtp>
#include <QFile>
//#include <QUrlInfo>
#include <QTimer>
#include <QFileInfo>
#include "qftp.h"
class FtpManager : public QObject
{
Q_OBJECT
public:
explicit FtpManager(QObject *parent = nullptr);
void connectToHost(const QString& host, int port, const QString& user, const QString& pass);
void uploadFile(const QString& fileName);
void downloadFile(const QString& remoteFile, const QString& localFile);
void uploadFolder(const QString &localFolderPath, const QString &remoteFolderPath);
void deleteFile(const QString& fileName);
void listDirectory();
void DelayMsec(unsigned int msec);
signals:
void fileListUpdated(const QStringList& files);
void progressUpdated(int percent);
void statusUpdated(const QString& status);
private slots:
void ftpCommandStarted(int id);
void ftpCommandFinished(int id, bool error);
void ftpListInfo(const QUrlInfo& urlInfo);
void ftpDataTransferProgress(qint64 done, qint64 total);
private:
QFtp* m_ftp;
QStringList m_fileList;
QString m_currentLocalFile;
QFile* m_file;
QTimer* m_refreshTimer;
};
#endif // FTPMANAGER_H
#include "ftpmanager.h"
#include <QMessageBox>
#include <QApplication>
FtpManager::FtpManager(QObject *parent)
: QObject(parent)
, m_ftp(new QFtp(this))
, m_file(nullptr)
, m_refreshTimer(new QTimer(this))
{
connect(m_ftp, &QFtp::commandStarted, this, &FtpManager::ftpCommandStarted);
connect(m_ftp, &QFtp::commandFinished, this, &FtpManager::ftpCommandFinished);
connect(m_ftp, &QFtp::listInfo, this, &FtpManager::ftpListInfo);
connect(m_ftp, &QFtp::dataTransferProgress, this, &FtpManager::ftpDataTransferProgress);
connect(m_refreshTimer, &QTimer::timeout, this, &FtpManager::listDirectory);
m_refreshTimer->setSingleShot(true);
}
void FtpManager::connectToHost(const QString& host, int port, const QString& user, const QString& pass)
{
emit statusUpdated("正在連接...");
m_ftp->connectToHost(host, port);
m_ftp->login(user, pass);
}
void FtpManager::uploadFile(const QString& fileName)
{
m_file = new QFile(fileName, this);
if (!m_file->open(QIODevice::ReadOnly)) {
emit statusUpdated("無法打開文件");
return;
}
QString remoteName = QFileInfo(fileName).fileName();
m_currentLocalFile = fileName;
emit statusUpdated(QString("正在上傳: %1").arg(remoteName));
m_ftp->put(m_file, remoteName);
}
void FtpManager::uploadFolder(const QString &localFolderPath, const QString &remoteFolderPath) {
QDir dir(localFolderPath);
QFileInfoList entries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
QString remoteEntryPath = remoteFolderPath + "/"+dir.dirName();
m_ftp->mkdir(remoteEntryPath);
DelayMsec(1000);
m_ftp->cd(remoteEntryPath);
DelayMsec(2000);
foreach (const QFileInfo &entryInfo, entries) {
QString localEntryPath = entryInfo.absoluteFilePath();
remoteEntryPath += "/" + entryInfo.fileName();
if (entryInfo.isFile()) { // 如果是文件,則上傳文件
uploadFile(localEntryPath);
DelayMsec(100);
// QFile file(localEntryPath);
// if (file.open(QIODevice::ReadOnly)) {
// m_ftp->put(&file, remoteEntryPath); // 上傳文件
// DelayMsec(2000);
// file.close();
// }
}
}
}
void FtpManager::downloadFile(const QString& remoteFile, const QString& localFile)
{
m_file = new QFile(localFile, this);
if (!m_file->open(QIODevice::WriteOnly)) {
emit statusUpdated("無法創(chuàng)建本地文件");
return;
}
m_currentLocalFile = localFile;
emit statusUpdated(QString("正在下載: %1").arg(remoteFile));
m_ftp->get(remoteFile, m_file);
}
void FtpManager::deleteFile(const QString& fileName)
{
emit statusUpdated(QString("正在刪除: %1").arg(fileName));
m_ftp->remove(fileName);
}
void FtpManager::listDirectory()
{
m_fileList.clear();
m_ftp->list();
}
void FtpManager::ftpCommandStarted(int id)
{
Q_UNUSED(id);
}
void FtpManager::ftpCommandFinished(int id, bool error)
{
Q_UNUSED(id);
if (error) {
emit statusUpdated(QString("錯(cuò)誤: %1").arg(m_ftp->errorString()));
if (m_file && m_file->isOpen()) {
m_file->close();
m_file->deleteLater();
m_file = nullptr;
}
return;
}
switch (m_ftp->currentCommand()) {
case QFtp::ConnectToHost:
emit statusUpdated("連接成功");
m_ftp->list();
break;
case QFtp::Login:
emit statusUpdated("登錄成功");
m_ftp->list();
break;
case QFtp::List:
emit fileListUpdated(m_fileList);
emit statusUpdated("目錄列表更新完成");
break;
case QFtp::Put:
if (m_file) {
m_file->close();
m_file->deleteLater();
m_file = nullptr;
}
emit statusUpdated("上傳完成");
m_refreshTimer->start(1000); // 延遲刷新
break;
case QFtp::Get:
if (m_file) {
m_file->close();
m_file->deleteLater();
m_file = nullptr;
}
emit statusUpdated("下載完成");
break;
case QFtp::Remove:
emit statusUpdated("刪除完成");
m_refreshTimer->start(1000); // 延遲刷新
break;
default:
break;
}
}
void FtpManager::ftpListInfo(const QUrlInfo& urlInfo)
{
if (urlInfo.name() != "." && urlInfo.name() != "..") {
QString item = urlInfo.name();
if (urlInfo.isDir()) {
item += "/";
}
m_fileList << item;
}
}
void FtpManager::ftpDataTransferProgress(qint64 done, qint64 total)
{
if (total > 0) {
int percent = static_cast<int>((done * 100) / total);
emit progressUpdated(percent);
}
}
void FtpManager::DelayMsec(unsigned int msec)
{
QEventLoop loop;
QTimer::singleShot(msec, &loop, SLOT(quit()));
loop.exec(); // 在這里會(huì)阻塞,直到QTimer超時(shí)退出循環(huán)
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QDockWidget>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QStyle>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
private slots:
private:
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QListWidget>
#include <QProgressBar>
#include <QFileDialog>
#include <QMessageBox>
#include "ftpmanager.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Qt5 QFtp客戶端");
resize(600, 500);
// 創(chuàng)建FTP管理器
FtpManager* ftpManager = new FtpManager(this);
// 服務(wù)器設(shè)置區(qū)域
QWidget* serverWidget = new QWidget(this);
serverWidget->setFixedSize(500,50);
QHBoxLayout* serverLayout = new QHBoxLayout(serverWidget);
QLabel* hostLabel = new QLabel("主機(jī):");
QLineEdit* hostEdit = new QLineEdit("ftp.example.com");
hostEdit->setObjectName("hostEdit");
QLabel* portLabel = new QLabel("端口:");
QLineEdit* portEdit = new QLineEdit("21");
portEdit->setObjectName("portEdit");
portEdit->setMaximumWidth(60);
QLabel* userLabel = new QLabel("用戶:");
QLineEdit* userEdit = new QLineEdit("anonymous");
userEdit->setObjectName("userEdit");
QLabel* passLabel = new QLabel("密碼:");
QLineEdit* passEdit = new QLineEdit();
passEdit->setObjectName("passEdit");
passEdit->setEchoMode(QLineEdit::Password);
QPushButton* connectBtn = new QPushButton("連接");
connectBtn->setObjectName("connectBtn");
serverLayout->addWidget(hostLabel);
serverLayout->addWidget(hostEdit);
serverLayout->addWidget(portLabel);
serverLayout->addWidget(portEdit);
serverLayout->addWidget(userLabel);
serverLayout->addWidget(userEdit);
serverLayout->addWidget(passLabel);
serverLayout->addWidget(passEdit);
serverLayout->addWidget(connectBtn);
// 文件操作按鈕
QWidget* controlWidget = new QWidget(this);
controlWidget->setFixedSize(500,50);
controlWidget->move(0,45);
QHBoxLayout* controlLayout = new QHBoxLayout(controlWidget);
QPushButton* uploadBtn = new QPushButton("上傳");
uploadBtn->setObjectName("uploadBtn");
QPushButton* downloadBtn = new QPushButton("下載");
downloadBtn->setObjectName("downloadBtn");
QPushButton* deleteBtn = new QPushButton("刪除");
deleteBtn->setObjectName("deleteBtn");
QPushButton* refreshBtn = new QPushButton("刷新");
refreshBtn->setObjectName("refreshBtn");
controlLayout->addWidget(uploadBtn);
controlLayout->addWidget(downloadBtn);
controlLayout->addWidget(deleteBtn);
controlLayout->addWidget(refreshBtn);
controlLayout->addStretch();
// 文件列表
QListWidget* fileList = new QListWidget(this);
fileList->setFixedSize(500,300);
fileList->move(20,100);
fileList->setObjectName("fileList");
// 進(jìn)度條
QProgressBar* progressBar = new QProgressBar(this);
progressBar->setObjectName("progressBar");
progressBar->setVisible(false);
// 狀態(tài)標(biāo)簽
QLabel* statusLabel = new QLabel("就緒",this);
statusLabel->move(20,400);
statusLabel->setObjectName("statusLabel");
// 布局
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addWidget(serverWidget);
mainLayout->addWidget(controlWidget);
mainLayout->addWidget(fileList);
mainLayout->addWidget(progressBar);
mainLayout->addWidget(statusLabel);
this->setLayout(mainLayout);
// 連接信號(hào)槽
QObject::connect(connectBtn, &QPushButton::clicked, [=]() {
QString host = hostEdit->text();
int port = portEdit->text().toInt();
QString user = userEdit->text();
QString pass = passEdit->text();
ftpManager->connectToHost(host, port, user, pass);
});
QObject::connect(uploadBtn, &QPushButton::clicked, [=]() {
QString fileName = QFileDialog::getOpenFileName(this, "選擇上傳文件");
if (!fileName.isEmpty()) {
ftpManager->uploadFile(fileName);
}
});
QObject::connect(downloadBtn, &QPushButton::clicked, [=]() {
QListWidgetItem* item = fileList->currentItem();
if (item) {
QString fileName = item->text();
QString savePath = QFileDialog::getSaveFileName(this, "保存文件", fileName);
if (!savePath.isEmpty()) {
ftpManager->downloadFile(fileName, savePath);
}
} else {
QMessageBox::warning(this, "警告", "請(qǐng)選擇要下載的文件");
}
});
QObject::connect(deleteBtn, &QPushButton::clicked, [=]() {
QListWidgetItem* item = fileList->currentItem();
if (item) {
QString fileName = item->text();
int ret = QMessageBox::question(this, "確認(rèn)", QString("確定刪除文件 %1?").arg(fileName));
if (ret == QMessageBox::Yes) {
ftpManager->deleteFile(fileName);
}
} else {
QMessageBox::warning(this, "警告", "請(qǐng)選擇要?jiǎng)h除的文件");
}
});
QObject::connect(refreshBtn, &QPushButton::clicked, [=]() {
ftpManager->listDirectory();
});
QObject::connect(ftpManager, &FtpManager::fileListUpdated, [=](const QStringList& files) {
fileList->clear();
fileList->addItems(files);
});
QObject::connect(ftpManager, &FtpManager::progressUpdated, [=](int percent) {
progressBar->setVisible(true);
progressBar->setValue(percent);
if (percent >= 100) {
QTimer::singleShot(1000, [=]() {
progressBar->setVisible(false);
});
}
});
QObject::connect(ftpManager, &FtpManager::statusUpdated, [=](const QString& status) {
statusLabel->setText(status);
});
}
MainWindow::~MainWindow()
{
}
#include <QApplication>
#include <QWidget>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
到此這篇關(guān)于QT5使用QFtp的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)QT5使用QFtp內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C語言實(shí)現(xiàn)BMP圖像處理(哈夫曼編碼)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BMP圖像哈夫曼編碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
用C語言實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了用C語言實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07

