2016-12-19 56 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我回答了他的錯誤,你在代碼中有更多的錯誤 –