gpt4 book ai didi

c++ - 在参数中传递 QCoreApplication

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:20 24 4
gpt4 key购买 nike

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

我创建了一个计时器,它与我的QCoreApplication 应用程序 的信号相关联,并在计时器达到 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);

};

请求.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 客户端 实例是一个局部变量,其生命周期由编译器管理。给它一个 parent 是没有意义的。 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) {
...
});
}

关于c++ - 在参数中传递 QCoreApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com