2016-12-19 19 views
0

我特林建立一个客户端的Web服务。我的目标是每秒向我的服务器发送请求。我用这个库,帮助我:QHttp通QCoreApplication参数

我创建了一个定时器,我用的信号链接到我的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的参数,这是正确的?

回答

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) { 
     ... 
    }); 
} 
+0

非常强烈的答案,并理解我的问题。我记得在4年前在这里问过一些问题,并从你那里得到答案,谢谢你的工作。 –

1

无法通过它的拷贝构造函数传递一个副本QCoreApplication对象,你必须通过一个指针QCoreApplication。

所有“QCoreApplication应用”应该由“QCoreApplication *应用程序”,并把这些函数的调用来替代必须包含一个指向应用程式不是它的一个副本。

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说,也有你的代码更多的问题,阅读他的回答

+0

这忽略了其他问题的代码和声明将无法正常工作。 –

+0

@KubaOber你能让它更清楚吗? –

+0

@KubaOber我回答了他的错误,你在代码中有更多的错误 –