Skip to content

Commit

Permalink
[Update Qt Module Names and Refactor HttpClient]: 更新 Qt 模块名称并重构 HttpC…
Browse files Browse the repository at this point in the history
…lient

将所有 CMakeLists.txt 文件中的 Qt6 模块名称更新为 Qt 模块名称,以适应新的 Qt 版本.
在 HttpClient 类中增加了新的上传方法 upload_put 和 upload_post,支持不同类型的上传操作.
重构了 HttpClient 的测试代码,增加了对上传和下载功能的测试用例.
  • Loading branch information
RealChuan committed Jan 10, 2025
1 parent 4165ea6 commit cd6a81b
Show file tree
Hide file tree
Showing 37 changed files with 442 additions and 227 deletions.
10 changes: 3 additions & 7 deletions Battery/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
batterywidget.h
batterywidget.cpp)
set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h batterywidget.h
batterywidget.cpp)

qt_add_executable(Battery MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(Battery PRIVATE Qt6::Widgets)
target_link_libraries(Battery PRIVATE Qt::Widgets)
qt_finalize_executable(Battery)
2 changes: 1 addition & 1 deletion BatteryQuick/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set_target_properties(
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE)

target_link_libraries(BatteryQuick PRIVATE Qt6::Quick)
target_link_libraries(BatteryQuick PRIVATE Qt::Quick)

include(GNUInstallDirs)
install(
Expand Down
2 changes: 1 addition & 1 deletion Bootstrap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ endif()
qt_add_resources(SOURCES resouce.qrc)

qt_add_executable(Bootstrap MANUAL_FINALIZATION ${PROJECT_SOURCES} ${SOURCES})
target_link_libraries(Bootstrap PRIVATE Qt6::Widgets)
target_link_libraries(Bootstrap PRIVATE Qt::Widgets)
qt_finalize_executable(Bootstrap)
10 changes: 3 additions & 7 deletions BubbleWindow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
bubblewidget.h
bubblewidget.cpp)
set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h bubblewidget.h
bubblewidget.cpp)

qt_add_executable(BubbleWindow MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(BubbleWindow PRIVATE Qt6::Widgets)
target_link_libraries(BubbleWindow PRIVATE Qt::Widgets)
qt_finalize_executable(BubbleWindow)
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ find_package(
Network
Concurrent
Sql
Quick)
Quick
Test)

qt_standard_project_setup(REQUIRES 6.5)
qt_policy(SET QTP0001 NEW)
Expand Down
2 changes: 1 addition & 1 deletion Chart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ set(PROJECT_SOURCES
stackedbarchart.cpp)

qt_add_executable(Chart MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(Chart PRIVATE Qt6::Widgets Qt6::Charts)
target_link_libraries(Chart PRIVATE Qt::Widgets Qt::Charts)
qt_finalize_executable(Chart)
12 changes: 4 additions & 8 deletions CheckBoxStandardItem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(PROJECT_SOURCES
main.cc
mainwindow.cc
mainwindow.hpp
checkboxstandarditem.hpp
checkboxstandarditem.cc)
set(PROJECT_SOURCES main.cc mainwindow.cc mainwindow.hpp
checkboxstandarditem.hpp checkboxstandarditem.cc)

qt_add_executable(CheckBoxStandardItem MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(CheckBoxStandardItem PRIVATE Qt6::Widgets)
qt_finalize_executable(CheckBoxStandardItem)
target_link_libraries(CheckBoxStandardItem PRIVATE Qt::Widgets)
qt_finalize_executable(CheckBoxStandardItem)
10 changes: 3 additions & 7 deletions Clock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
clockwidget.h
clockwidget.cpp)
set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h clockwidget.h
clockwidget.cpp)

qt_add_executable(Clock MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(Clock PRIVATE Qt6::Widgets)
target_link_libraries(Clock PRIVATE Qt::Widgets)
qt_finalize_executable(Clock)
10 changes: 3 additions & 7 deletions DashBoard/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
dashboardwidget.h
dashboardwidget.cpp)
set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h dashboardwidget.h
dashboardwidget.cpp)

qt_add_executable(DashBoard MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(DashBoard PRIVATE Qt6::Widgets)
target_link_libraries(DashBoard PRIVATE Qt::Widgets)
qt_finalize_executable(DashBoard)
16 changes: 8 additions & 8 deletions DragDrop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
draglistwidget.h
draglistwidget.cpp
droplistwidget.h
droplistwidget.cpp)
main.cpp
mainwindow.cpp
mainwindow.h
draglistwidget.h
draglistwidget.cpp
droplistwidget.h
droplistwidget.cpp)

qt_add_executable(DragDrop MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(DragDrop PRIVATE Qt6::Widgets)
target_link_libraries(DragDrop PRIVATE Qt::Widgets)
qt_finalize_executable(DragDrop)
2 changes: 1 addition & 1 deletion FlowLayout/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ set(PROJECT_SOURCES
flowwidget.cc)

qt_add_executable(FlowLayout MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(FlowLayout PRIVATE Qt6::Widgets)
target_link_libraries(FlowLayout PRIVATE Qt::Widgets)
qt_finalize_executable(FlowLayout)
16 changes: 8 additions & 8 deletions GridViewModel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
gridmodel.h
gridmodel.cpp
gridview.h
gridview.cpp)
main.cpp
mainwindow.cpp
mainwindow.h
gridmodel.h
gridmodel.cpp
gridview.h
gridview.cpp)

qt_add_executable(GridViewModel MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(GridViewModel PRIVATE Qt6::Widgets)
target_link_libraries(GridViewModel PRIVATE Qt::Widgets)
qt_finalize_executable(GridViewModel)
2 changes: 1 addition & 1 deletion HttpClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set(PROJECT_SOURCES main.cpp httpclient.cc httpclient.hpp)

qt_add_executable(HttpClient MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(HttpClient PRIVATE Qt6::Network Qt6::Concurrent)
target_link_libraries(HttpClient PRIVATE Qt::Network Qt::Concurrent Qt::Test)
qt_finalize_executable(HttpClient)
2 changes: 1 addition & 1 deletion HttpClient/HttpClient.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
QT += core gui network
QT += core gui network testlib

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand Down
108 changes: 83 additions & 25 deletions HttpClient/httpclient.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "httpclient.hpp"

#include <QEventLoop>
#include <QFile>
#include <QFileInfo>
#include <QHttpMultiPart>
#include <QJsonDocument>
#include <QJsonObject>
#include <QPointer>
Expand Down Expand Up @@ -168,7 +169,7 @@ QNetworkReply *HttpClient::downLoad(const QUrl &url,
const QByteArray fromRange = "bytes=" + QByteArray::number(bytes) + "-";
request.setRawHeader("Range", fromRange);
}
qDebug() << QString("Download: %-1>%2").arg(url.toString(QUrl::RemoveUserInfo), filePath);
qDebug() << QString("Download: %1->%2").arg(url.toString(QUrl::RemoveUserInfo), filePath);

auto *reply = QNetworkAccessManager::get(request);
d_ptr->downloads
Expand All @@ -187,16 +188,17 @@ QNetworkReply *HttpClient::downLoad(const QUrl &url,
return reply;
}

QNetworkReply *HttpClient::upload(const QUrl &url,
const QString &filePath,
int timeout,
bool verifyCertificate,
CallBack callBack)
QNetworkReply *HttpClient::upload_put(const QUrl &url,
const QString &filePath,
int timeout,
bool verifyCertificate,
CallBack callBack)
{
Q_ASSERT(!filePath.isEmpty());
auto *file = new QFile(filePath, this);
if (!file->open(QIODevice::ReadOnly)) {
qWarning() << QString("Cannot open the file: %1!").arg(filePath) << file->errorString();
file->deleteLater();
return nullptr;
}
qDebug() << QString("Upload: %1->%2").arg(filePath, url.toString(QUrl::RemoveUserInfo));
Expand All @@ -206,35 +208,78 @@ QNetworkReply *HttpClient::upload(const QUrl &url,
auto *reply = QNetworkAccessManager::put(request, file);
file->setParent(reply);
d_ptr->uploads.insert(reply, file);
d_ptr->tasks.insert(reply, callBack);
connect(reply, &QNetworkReply::errorOccurred, this, &HttpClient::onErrorOccurred);
connect(reply, &QNetworkReply::sslErrors, this, &HttpClient::onSslErrors);
connect(reply, &QNetworkReply::finished, this, &HttpClient::onUploadFinish);
if (timeout > 0) {
auto *timer = new QTimer(reply);
connect(timer, &QTimer::timeout, this, &HttpClient::onNetworkTimeout);
timer->start(timeout * 1000);
}
connectUploadSlots(reply, timeout, callBack);
return reply;
}

QNetworkReply *HttpClient::upload(
QNetworkReply *HttpClient::upload_put(
const QUrl &url, const QByteArray &data, int timeout, bool verifyCertificate, CallBack callBack)
{
qDebug() << QString("Upload To %1").arg(url.toString(QUrl::RemoveUserInfo));

auto request = d_ptr->networkRequest(verifyCertificate);
request.setUrl(url);
auto *reply = QNetworkAccessManager::put(request, data);
d_ptr->tasks.insert(reply, callBack);
connect(reply, &QNetworkReply::errorOccurred, this, &HttpClient::onErrorOccurred);
connect(reply, &QNetworkReply::sslErrors, this, &HttpClient::onSslErrors);
connect(reply, &QNetworkReply::finished, this, &HttpClient::onUploadFinish);
if (timeout > 0) {
auto *timer = new QTimer(reply);
connect(timer, &QTimer::timeout, this, &HttpClient::onNetworkTimeout);
timer->start(timeout * 1000);
connectUploadSlots(reply, timeout, callBack);
return reply;
}

QNetworkReply *HttpClient::upload_post(const QUrl &url,
const QString &filePath,
int timeout,
bool verifyCertificate,
CallBack callBack)
{
Q_ASSERT(!filePath.isEmpty());
auto *file = new QFile(filePath, this);
if (!file->open(QIODevice::ReadOnly)) {
qWarning() << QString("Cannot open the file: %1!").arg(filePath) << file->errorString();
file->deleteLater();
return nullptr;
}
auto filename = QFileInfo(filePath).fileName();
qDebug() << QString("Upload: %1->%2")
.arg(filePath, url.toString(QUrl::RemoveUserInfo) + "/" + filename);

auto disposition = QString("form-data; name=\"%1\"; filename=\"%2\"").arg("file", filename);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(disposition));
filePart.setBodyDevice(file);
auto *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
multiPart->append(filePart);

auto request = d_ptr->networkRequest(verifyCertificate);
request.setUrl(url);

auto *reply = QNetworkAccessManager::post(request, multiPart);
file->setParent(reply);
multiPart->setParent(reply);
d_ptr->uploads.insert(reply, file);
connectUploadSlots(reply, timeout, callBack);
return reply;
}

QNetworkReply *HttpClient::upload_post(const QUrl &url,
const QString &filename,
const QByteArray &data,
int timeout,
bool verifyCertificate,
CallBack callBack)
{
qDebug() << QString("Upload To %1").arg(url.toString(QUrl::RemoveUserInfo) + "/" + filename);
auto disposition = QString("form-data; name=\"%1\"; filename=\"%2\"").arg("file", filename);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(disposition));
filePart.setBody(data);
auto *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
multiPart->append(filePart);

auto request = d_ptr->networkRequest(verifyCertificate);
request.setUrl(url);

auto *reply = QNetworkAccessManager::post(request, multiPart);
multiPart->setParent(reply);
connectUploadSlots(reply, timeout, callBack);
return reply;
}

Expand Down Expand Up @@ -368,6 +413,19 @@ QJsonObject HttpClient::hookResult(const QJsonObject &object)
return object;
}

void HttpClient::connectUploadSlots(QNetworkReply *reply, int timeout, CallBack callBack)
{
d_ptr->tasks.insert(reply, callBack);
connect(reply, &QNetworkReply::errorOccurred, this, &HttpClient::onErrorOccurred);
connect(reply, &QNetworkReply::sslErrors, this, &HttpClient::onSslErrors);
connect(reply, &QNetworkReply::finished, this, &HttpClient::onUploadFinish);
if (timeout > 0) {
auto *timer = new QTimer(reply);
connect(timer, &QTimer::timeout, this, &HttpClient::onNetworkTimeout);
timer->start(timeout * 1000);
}
}

void HttpClient::queryResult(QNetworkReply *reply, const QJsonObject &object)
{
qDebug() << object;
Expand Down
32 changes: 22 additions & 10 deletions HttpClient/httpclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,27 @@ class HttpClient : public QNetworkAccessManager
ProgressCallBack progressCallBack = nullptr,
CallBack callBack = nullptr);

QNetworkReply *upload(const QUrl &url,
const QString &filePath,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);
QNetworkReply *upload(const QUrl &url,
const QByteArray &data,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);
QNetworkReply *upload_put(const QUrl &url,
const QString &filePath,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);
QNetworkReply *upload_put(const QUrl &url,
const QByteArray &data,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);
QNetworkReply *upload_post(const QUrl &url,
const QString &filePath,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);
QNetworkReply *upload_post(const QUrl &url,
const QString &filename,
const QByteArray &data,
int timeout = -1,
bool verifyCertificate = true,
CallBack callBack = nullptr);

signals:
void timeOut();
Expand All @@ -63,6 +74,7 @@ private slots:
virtual QJsonObject hookResult(const QJsonObject &object);

private:
void connectUploadSlots(QNetworkReply *reply, int timeout, CallBack callBack);
void queryResult(QNetworkReply *reply, const QJsonObject &object);

class HttpClientPrivate;
Expand Down
Loading

0 comments on commit cd6a81b

Please sign in to comment.