我要为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,这是对的吗?
答案 0 :(得分:4)
首先,如果您需要访问全局应用程序实例,则不应传递它。使用qApp
宏或QCoreApplication::instance()
。
但除此之外:QHttpClient client
实例是一个局部变量,它的生命周期由编译器管理。给它一个父母没有意义。当client
退出时,sendRequest
会被销毁:由于此原因,您的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
。如果没有,那么您必须将QHttpClient
实例保持在main
:
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) {
...
});
}
答案 1 :(得分:1)
您无法通过其复制构造函数传递复制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所说,你的代码中存在更多问题,请阅读他的回答