0

I used QNetworkAccessManager to handle send a http request:

void f()
{    
    QNetworkRequest request( QUrl( address ) );    
    QNetworkAccessManager mng;
    mng.get(request);
    //QEventLoop().exec();
    }
void main()
{
    ...
    f();
    ...
}

If without line QEventLoop().exec();, the app will not send request. But if with line QEventLoop().exec();, it will. I don't understan why?
Can you clear me?
Thank you very much! (P/s: I have seen this link: Sending an HTTP request using QNetworkAccessManager)

Community
  • 1
  • 1
aviit
  • 1,957
  • 1
  • 27
  • 50

1 Answers1

0
void f()    
{        
    static QNetworkRequest request( QUrl( address ) );        
    static QNetworkAccessManager mng;
    mng.get(request);
}
void main()
{
    ...
    f();
    ...
}

I think this is because QNetworkAccessManager is asynchronous and need the time to send the request. But require, url, query, ... is destroyed when out of scope. -> we need in scope when QNetworkAccessManager do send request (use QEventLoop().exec(); ) or let require, url, query, ... is persistent (declare it with static).

aviit
  • 1,957
  • 1
  • 27
  • 50