2016-12-19 2 views
0

Webサービスのクライアントをビルドしています。私の目標は、毎秒私のサーバーに要求を送信することですパラメータでQCoreApplicationを渡します。

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をPASSEことができないという事実から来ていると仮定し、この権利はありますか?

答えて

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を使用するようにコードを再設計する必要があります。ない場合は、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) { 
     ... 
    }); 
} 
+0

非常に強い答え、私の問題の理解。私はここ4年前に何かを尋ねてきたことを覚えています。 –

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が言ったように、あなたのコード内の多くの問題があります、彼の答えを読む

+0

これは、コードと他の問題をミスして述べたように動作しません。 –

+0

@KubaOberより明確にしてください。 –

+0

@KubaOber私は彼のエラーに答えた、あなたはすぐにコードにエラーがあります。 –

関連する問題