diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b62b0884..3b1ef573 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -93,6 +93,11 @@ qt_add_qml_module(example RESOURCES ${resource_files} ) +#导入component头文件,不然通过QML_NAMED_ELEMENT生成的c++类会找不到头文件报错 +target_include_directories(example PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src/component +) + #设置属性 set_target_properties(example PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com diff --git a/example/qml/page/T_Http.qml b/example/qml/page/T_Http.qml index 1b0eda03..07804936 100644 --- a/example/qml/page/T_Http.qml +++ b/example/qml/page/T_Http.qml @@ -11,6 +11,9 @@ FluScrollablePage{ title:"Http" + Component.onDestruction: { + console.debug("T_Http -> onDestruction") + } FluHttp{ id:http_get @@ -66,6 +69,7 @@ FluScrollablePage{ onDownloadProgress: (recv,total)=>{ var precent = (recv/total * 100).toFixed(0) + "%" + console.debug(precent) btn_download.text = "下载中..."+precent } onError: diff --git a/example/src/component/CircularReveal.h b/example/src/component/CircularReveal.h index 675dc73f..5084b165 100644 --- a/example/src/component/CircularReveal.h +++ b/example/src/component/CircularReveal.h @@ -12,6 +12,7 @@ class CircularReveal : public QQuickPaintedItem Q_OBJECT Q_PROPERTY_AUTO(QQuickItem*,target) Q_PROPERTY_AUTO(int,radius) + QML_NAMED_ELEMENT(CircularReveal) public: CircularReveal(QQuickItem* parent = nullptr); void paint(QPainter* painter) override; diff --git a/example/src/component/FileWatcher.h b/example/src/component/FileWatcher.h index 84919492..5db1df83 100644 --- a/example/src/component/FileWatcher.h +++ b/example/src/component/FileWatcher.h @@ -3,12 +3,14 @@ #include #include +#include #include "src/stdafx.h" class FileWatcher : public QObject { Q_OBJECT Q_PROPERTY_AUTO(QString,path); + QML_NAMED_ELEMENT(FileWatcher) public: explicit FileWatcher(QObject *parent = nullptr); Q_SIGNAL void fileChanged(); diff --git a/example/src/main.cpp b/example/src/main.cpp index 1d51ae86..2d559034 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -8,8 +8,6 @@ #include #include #include -#include "src/component/CircularReveal.h" -#include "src/component/FileWatcher.h" #include "AppInfo.h" FRAMELESSHELPER_USE_NAMESPACE @@ -39,8 +37,6 @@ FRAMELESSHELPER_USE_NAMESPACE #ifdef FLUENTUI_BUILD_STATIC_LIB engine.addImportPath("qrc:/"); // 让静态资源可以被QML引擎搜索到 #endif - qmlRegisterType("example", 1, 0, "CircularReveal"); - qmlRegisterType("example", 1, 0, "FileWatcher"); appInfo->init(&engine); const QUrl url(QStringLiteral("qrc:/example/qml/App.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, diff --git a/src/FluHttp.cpp b/src/FluHttp.cpp index be38e259..00c70a98 100644 --- a/src/FluHttp.cpp +++ b/src/FluHttp.cpp @@ -12,13 +12,32 @@ FluHttp::FluHttp(QObject *parent) enabledBreakpointDownload(false); timeout(15); retry(3); + } -Q_INVOKABLE void FluHttp::postString(QString params,QVariantMap headers){ +FluHttp::~FluHttp(){ + cancel(); +} + +void FluHttp::cancel(){ + foreach (auto item, cache) { + if(item){ + qDebug()<abort(); + } + } +} + +void FluHttp::handleReply(QNetworkReply* reply){ + cache.append(reply); +} + +void FluHttp::postString(QString params,QVariantMap headers){ QVariantMap request = invokeIntercept(params,headers,"postString").toMap(); QThreadPool::globalInstance()->start([=](){ - HttpClient client; Q_EMIT start(); + HttpClient client; + client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); }; client.post(_url) .retry(retry()) .timeout(timeout()) @@ -26,22 +45,22 @@ Q_INVOKABLE void FluHttp::postString(QString params,QVariantMap headers){ .headers(request["headers"].toMap()) .onSuccess([=](QString result) { Q_EMIT success(result); - Q_EMIT finish(); }) .onFailed([=](QNetworkReply* reply) { Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString()); - Q_EMIT finish(); }) .block() .exec(); + Q_EMIT finish(); }); } void FluHttp::post(QVariantMap params,QVariantMap headers){ QVariantMap request = invokeIntercept(params,headers,"post").toMap(); QThreadPool::globalInstance()->start([=](){ - HttpClient client; Q_EMIT start(); + HttpClient client; + client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); }; client.post(_url) .retry(retry()) .timeout(timeout()) @@ -50,22 +69,22 @@ void FluHttp::post(QVariantMap params,QVariantMap headers){ .headers(request["headers"].toMap()) .onSuccess([=](QString result) { Q_EMIT success(result); - Q_EMIT finish(); }) .onFailed([=](QNetworkReply* reply) { Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString()); - Q_EMIT finish(); }) .block() .exec(); + Q_EMIT finish(); }); } void FluHttp::postJson(QVariantMap params,QVariantMap headers){ QVariantMap request = invokeIntercept(params,headers,"postJson").toMap(); QThreadPool::globalInstance()->start([=](){ - HttpClient client; Q_EMIT start(); + HttpClient client; + client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); }; client.post(_url) .retry(retry()) .timeout(timeout()) @@ -74,22 +93,22 @@ void FluHttp::postJson(QVariantMap params,QVariantMap headers){ .headers(request["headers"].toMap()) .onSuccess([=](QString result) { Q_EMIT success(result); - Q_EMIT finish(); }) .onFailed([=](QNetworkReply* reply) { Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString()); - Q_EMIT finish(); }) .block() .exec(); + Q_EMIT finish(); }); } void FluHttp::get(QVariantMap params,QVariantMap headers){ QVariantMap request = invokeIntercept(params,headers,"get").toMap(); QThreadPool::globalInstance()->start([=](){ - HttpClient client; Q_EMIT start(); + HttpClient client; + client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); }; client.get(_url) .retry(retry()) .timeout(timeout()) @@ -97,22 +116,23 @@ void FluHttp::get(QVariantMap params,QVariantMap headers){ .headers(request["headers"].toMap()) .onSuccess([=](QString result) { Q_EMIT success(result); - Q_EMIT finish(); }) .onFailed([=](QNetworkReply* reply) { Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString()); - Q_EMIT finish(); }) .block() .exec(); + Q_EMIT finish(); }); } -Q_INVOKABLE void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){ +void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){ + QPointer weakThis(this); QVariantMap request = invokeIntercept(params,headers,"download").toMap(); QThreadPool::globalInstance()->start([=](){ - HttpClient client; Q_EMIT start(); + HttpClient client; + client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); }; client.get(_url) .retry(retry()) .timeout(timeout()) @@ -121,18 +141,20 @@ Q_INVOKABLE void FluHttp::download(QString path,QVariantMap params,QVariantMap h .queryParams(request["params"].toMap()) .headers(request["headers"].toMap()) .onDownloadProgress([=](qint64 recv, qint64 total) { - Q_EMIT downloadProgress(recv,total); + if (!weakThis) { + return; + } + Q_EMIT weakThis->downloadProgress(recv,total); }) .onDownloadFileSuccess([=](QString result) { Q_EMIT success(result); - Q_EMIT finish(); }) .onDownloadFileFailed([=](QString errorString) { Q_EMIT error(-1,errorString); - Q_EMIT finish(); }) .block() .exec(); + Q_EMIT finish(); }); } diff --git a/src/FluHttp.h b/src/FluHttp.h index a1141e69..dc774f80 100644 --- a/src/FluHttp.h +++ b/src/FluHttp.h @@ -16,8 +16,11 @@ class FluHttp : public QObject QML_NAMED_ELEMENT(FluHttp) private: QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method); + void handleReply(QNetworkReply* reply); + QList cache; public: explicit FluHttp(QObject *parent = nullptr); + ~FluHttp(); Q_SIGNAL void start(); Q_SIGNAL void finish(); Q_SIGNAL void error(int status,QString errorString); @@ -27,8 +30,8 @@ public: Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {}); Q_INVOKABLE void postJson(QVariantMap params = {},QVariantMap headers = {}); Q_INVOKABLE void postString(QString params = "",QVariantMap headers = {}); - Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {}); + Q_INVOKABLE void cancel(); }; #endif // FLUHTTP_H diff --git a/src/HttpClient.h b/src/HttpClient.h index 6e33122c..cc16ef56 100644 --- a/src/HttpClient.h +++ b/src/HttpClient.h @@ -51,6 +51,9 @@ class HttpClient : public QNetworkAccessManager Q_OBJECT public: inline static HttpClient* instance(); + ~HttpClient(){ + qDebug()<<"HttpClient析构了"; + } inline HttpClient(QObject* parent = nullptr); inline QString getVersion() const; @@ -61,20 +64,7 @@ public: inline HttpRequest send(const QString& url, Operation op = GetOperation); - HttpClient* timeout(const int& second = 60) - { - timeoutSecond = second; - return this; - } - int timeoutSecond = 60; - - HttpClient* header(const QString& key, const QVariant& value) - { - globalHeader.insert(key, value); - return this; - } - QMap globalHeader; - + std::function initReplyCompleted; private: #if (QT_VERSION < QT_VERSION_CHECK(5, 8, 0)) inline QNetworkReply* sendCustomRequest(const QNetworkRequest& request, const QByteArray& verb, @@ -1347,9 +1337,10 @@ HttpResponse* HttpRequest::exec(const HttpRequest& _httpRequest, HttpResponse* h { httpRequest.m_reply->setReadBufferSize(httpRequest.m_readBufferSize); } - printDebug(httpRequest.m_logLevel, toString().toStdString().c_str()); - + if(httpClient->initReplyCompleted){ + httpClient->initReplyCompleted(httpRequest.m_reply); + } if (httpResponse) { httpResponse->setParent(httpRequest.m_reply); @@ -1564,27 +1555,27 @@ QString HttpClient::getVersion() const HttpRequest HttpClient::head(const QString& url) { - return HttpRequest(QNetworkAccessManager::HeadOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond); + return HttpRequest(QNetworkAccessManager::HeadOperation, this).url(url); } HttpRequest HttpClient::get(const QString& url) { - return HttpRequest(QNetworkAccessManager::GetOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond); + return HttpRequest(QNetworkAccessManager::GetOperation, this).url(url); } HttpRequest HttpClient::post(const QString& url) { - return HttpRequest(QNetworkAccessManager::PostOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond); + return HttpRequest(QNetworkAccessManager::PostOperation, this).url(url); } HttpRequest HttpClient::put(const QString& url) { - return HttpRequest(QNetworkAccessManager::PutOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond); + return HttpRequest(QNetworkAccessManager::PutOperation, this).url(url); } HttpRequest HttpClient::send(const QString& url, QNetworkAccessManager::Operation op) { - return HttpRequest(op, this).headers(globalHeader).url(url).timeout(timeoutSecond); + return HttpRequest(op, this).url(url); } #if (QT_VERSION < QT_VERSION_CHECK(5, 8, 0)) @@ -1791,12 +1782,10 @@ void HttpResponse::setHttpRequest(const HttpRequest& httpRequest) } } } - if (reply && httpRequest.m_isBlock) { new HttpBlocker(reply, httpRequest.m_isBlock); } - HttpRequest oldRequest = m_httpRequest; m_httpRequest = httpRequest;