5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

QtのQNetworkAccessManagerで同期通信

Last updated at Posted at 2012-11-12

QNetworkAccessManagerで通信し、結果を受け取るにはQNetworkAccessManager::finishedシグナルを受け取らなければならない。コードで書くとこうなる。

void ClassName::get() {
    QNetworkAccessManager *manager = new QNetworkAccessManager();

    connect(manager SIGNAL(finished(QNetworkReply*)),
            this, SLOT(finishSlot(QNetworkReply*)));
    manager->get(QUrl("http://127.0.0.1/index.html"));
}

// SLOT
void ClassName::finishedSlot(QNetworkReply *reply) {
    // 受信結果replyをゴニョゴニョ
}

同期通信したい場合は、QEventLoopを使用する。

void ClassName::get() {
    QNetworkAccessManager *manager = new QNetworkAccessManager();

    QEventLoop eventLoop;
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            &eventLoop, SLOT(quit()));
    QNetworkReply *reply = manager->get(QUrl("http://127.0.0.1/index.html"));
    eventLoop.exec();		// eventLoop.exec()終了後、replyにはGETの結果が格納されている

    // 続けて受信結果replyをゴニョゴニョできる
}

参考文献

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?