QNetworkAccessManager without finished signal

拟墨画扇 提交于 2020-06-28 12:38:04

问题


I want to request and response directly instead of using connect(..,SLOT(finished()),..,SLOT())


回答1:


You can use a QEventLoop so that the application waits and can handle other events at the same time.

#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;
}


来源:https://stackoverflow.com/questions/49869092/qnetworkaccessmanager-without-finished-signal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!