在参数中传递 QCoreApplication

Pass QCoreApplication in parameter

我正在尝试为 Web 服务构建客户端。我的目标是 每秒向我的服务器发送一个请求 。我用这个库来帮助我:QHttp

我创建了一个 计时器,我 link 向我的 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.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,对吗?

您不能通过复制构造函数传递复制 QCoreApplication 对象,您必须传递指向 QCoreApplication 的指针。

所有 "QCoreApplication app" 都应替换为 "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 所说,您的代码中还有更多问题,请阅读他的回答

首先,如果您需要访问全局应用程序实例,则不应传递它。使用 qApp 宏,或 QCoreApplication::instance().

但除此之外:QHttpClient client 实例是一个局部变量,其生命周期由编译器管理。给它一个 parent 是没有意义的。 clientsendRequest 退出时被销毁:因此你的 sendRequest 实际上是一个 no-op。

您的 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) {
      ...
    });
}