簡體   English   中英

在參數中傳遞QCoreApplication

[英]Pass QCoreApplication in parameter

我正在努力為Web服務構建客戶端。 我的目標是每秒向我的服務器發送一個請求 我用這個庫來幫助我: QHttp

我創建了一個計時器,計時器與信號鏈接到我的QCoreApplication app ,並在計時器達到1秒時發送我的請求

這是我的方法

main.cpp

#include "request.h"

int main(int argc, char** argv) {

    QCoreApplication app(argc, argv);
    Request* request = new Request();
    request->sendRequestPeriodically(1000, app);


    return app.exec();
}

request.h

//lots of include before
class Request
{
    Q_OBJECT

public:
    Request();
    void sendRequestPeriodically (int time, QCoreApplication app);

public slots:
    void sendRequest (QCoreApplication app);

};

request.cpp

#include "request.h"

void Request::sendRequest (QCoreApplication app){
    using namespace qhttp::client;
    QHttpClient client(&app);
    QUrl        server("http://127.0.0.1:8080/?Clearance");

    client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
        // response handler, called when the incoming HTTP headers are ready


        // gather HTTP response data (HTTP body)
        res->collectData();

        // when all data in HTTP response have been read:
        res->onEnd([res]() {
            // print the XML body of the response
            qDebug("\nreceived %d bytes of http body:\n%s\n",
                    res->collectedData().size(),
                    res->collectedData().constData()
                  );

            // done! now quit the application
            //qApp->quit();
        });

    });

    // set a timeout for the http connection
    client.setConnectingTimeOut(10000, []{
        qDebug("connecting to HTTP server timed out!");
        qApp->quit();
    });
}

void Request::sendRequestPeriodically(int time, QCoreApplication app){
    QTimer *timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
    timer->start(time); //time specified in ms
}

Request::Request()
{

}

我得到這些錯誤:

C:\Qt\5.7\mingw53_32\include\QtCore\qcoreapplication.h:211: erreur : 'QCoreApplication::QCoreApplication(const QCoreApplication&)' is private
     Q_DISABLE_COPY(QCoreApplication)

C:\Users\ebelloei\Documents\qhttp\example\client-aircraft\main.cpp:7: erreur : use of deleted function 'QCoreApplication::QCoreApplication(const QCoreApplication&)'

我是Qt的新手,但是我認為這是因為我無法在參數中傳遞QCoreApplication,對嗎?

首先,如果您需要訪問全局應用程序實例,則不應該傳遞它。 使用qApp宏或QCoreApplication::instance()

但是,除此之外, QHttpClient client實例是一個局部變量,其生存期由編譯器管理。 給它父母沒有任何意義。 clientsendRequest退出時立即被銷毀:因此,您的sendRequest實際上是無操作的。

您的connect語句中也有錯誤:您不能使用舊式的connect語法傳遞參數值:

// Wrong: the connection fails and does nothing.
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(foo)));
// Qt 5
connect(timer, &QTimer::timeout, this, [=]{ sendRequest(foo); });
// Qt 4
this->foo = foo;
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
  // sendRequest would then use this->foo

理想情況下,您應該重新設計代碼以使用QNetworkAccessManager 如果沒有,那么您必須在main內保持QHttpClient實例處於活動狀態:

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);
    qhttp::client::QHttpClient client;
    auto request = new Request(&client);
    request->sendRequestPeriodically(1000);
    return app.exec();
}

接着:

class Request : public QObject
{
    Q_OBJECT
    QTimer m_timer{this};
    qhttp::client::QHttpClient *m_client;
public:
    explicit Request(qhttp::client::QHttpClient *client);
    void sendRequestPeriodically(int time);
    void sendRequest();
};

Request::Request(qhttp::client::QHttpClient *client) :
    QObject{client},
    m_client{client}
{
    QObject::connect(&m_timer, &QTimer::timeout, this, &Request::sendRequest);
}

void Request::sendRequestPeriodically(int time) {
    timer->start(time);
}

void Request::sendRequest() {
    QUrl server("http://127.0.0.1:8080/?Clearance");

    m_client->request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
      ...
    });
}

您不能通過其副本構造函數傳遞副本QCoreApplication對象,而必須傳遞指向QCoreApplication的指針。

所有“ QCoreApplication應用程序”都應替換為“ QCoreApplication * app”,並且對這些函數的調用必須包含指向應用程序的指針,而不是其副本。

request.h

//lots of include before
class Request
{
    Q_OBJECT

public:
    Request();
    void sendRequestPeriodically (int time, QCoreApplication *app);

public slots:
    void sendRequest (QCoreApplication *app);

};

request.cpp

#include "request.h"

void Request::sendRequest (QCoreApplication *app){
    using namespace qhttp::client;
    QHttpClient client(app);
    QUrl        server("http://127.0.0.1:8080/?Clearance");

    client.request(qhttp::EHTTP_GET, server, [](QHttpResponse* res) {
        // response handler, called when the incoming HTTP headers are ready


        // gather HTTP response data (HTTP body)
        res->collectData();

        // when all data in HTTP response have been read:
        res->onEnd([res]() {
            // print the XML body of the response
            qDebug("\nreceived %d bytes of http body:\n%s\n",
                    res->collectedData().size(),
                    res->collectedData().constData()
                  );

            // done! now quit the application
            //qApp->quit();
        });

    });

    // set a timeout for the http connection
    client.setConnectingTimeOut(10000, []{
        qDebug("connecting to HTTP server timed out!");
        qApp->quit();
    });
}

void Request::sendRequestPeriodically(int time, QCoreApplication app){
    QTimer *timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest(app)));
    timer->start(time); //time specified in ms
}

Request::Request()
{

}

main.cpp

#include "request.h"

int main(int argc, char** argv) {

    QCoreApplication app(argc, argv);
    Request* request = new Request();
    request->sendRequestPeriodically(1000, &app);

    return app.exec();
}

編輯正如KubaOber說,也有你的代碼更多的問題,看他的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM