c++qtqt5qtnetwork

QNetworkAccessManager without finished signal


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


Solution

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