我想直接请求和回复,而不是使用connect(..,SLOT(finished()),..,SLOT())
答案 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;
}