문제

How would I write a function to wait for POST to complete? I am logging into a website that has multiple redirects and using connect() with QNetworkReply() leads to infinite GET requests.

I'm using a wait timer right now:

test->sendPostRequest(url, loginData);

    QEventLoop loop;
    QTimer::singleShot(8000, &loop, SLOT(quit()));
    loop.exec();


   test->sendGetRequest(request);
도움이 되었습니까?

해결책

You can use a local event loop using QEventLoop and connecting the signal finished of your QNetworkAccessManager to the quit slot of QEventLoop. This way when the post is finished, the local event loop quits and the rest gets executed :

QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);

networkManager->post(QNetworkRequest(serviceUrl), postData);


QEventLoop loop;
loop.connect(networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); 
loop.exec();

//...

I should not that it's the standard pattern for "blocking wait without blocking the UI".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top