首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数传递QCoreApplication

参数传递QCoreApplication
EN

Stack Overflow用户
提问于 2016-12-19 14:34:22
回答 2查看 870关注 0票数 0

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

我创建一个用信号链接到我的QCoreApplication app**,的计时器,并在计时器达到1秒*时发送请求。

下面是我是如何做到的,:

main.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
//lots of include before
class Request
{
    Q_OBJECT

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

public slots:
    void sendRequest (QCoreApplication app);

};

request.cpp

代码语言:javascript
复制
#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()
{

}

我发现了这些错误:

代码语言:javascript
复制
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,对吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-19 17:09:22

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

但这不是重点:QHttpClient client实例是一个局部变量,它的生存期由编译器管理。没有必要给它父母。当client退出时,sendRequest就会被销毁:由于这一点,您的sendRequest实际上是不操作的。

您的connect语句中也有错误:不能使用旧的样式connect语法传递参数值:

代码语言:javascript
复制
// 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。如果没有,则必须将QHttpClient实例保持在main中。

代码语言:javascript
复制
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();
}

然后:

代码语言:javascript
复制
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) {
      ...
    });
}
票数 4
EN

Stack Overflow用户

发布于 2016-12-19 14:59:12

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

所有"QCoreApplication应用程序“都应该替换为"QCoreApplication * app”,对这些函数的调用必须包括一个指向应用程序的指针,而不是它的副本。

request.h

代码语言:javascript
复制
//lots of include before
class Request
{
    Q_OBJECT

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

public slots:
    void sendRequest (QCoreApplication *app);

};

request.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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所说,您的代码中还有更多的问题,请阅读他的答案

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41224880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档