QNetworkAccessManager没有完成信号

时间:2018-04-17 02:46:33

标签: c++ qt qt5 qtnetwork

我想直接请求和回复,而不是使用connect(..,SLOT(finished()),..,SLOT())

1 个答案:

答案 0 :(得分:1)

您可以使用QEventLoop以便应用程序等待并可以同时处理其他事件。

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QNetworkAccessManager *manager = new QNetworkAccessManager;
    QEventLoop loop;
    QObject::connect(manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
    QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("https://api.github.com/events")));
    loop.exec();

    qDebug()<<reply->readAll();

    delete reply;
    delete manager;

    return 0;
}