Hi Guys,
With the following Qt Code I am trying to send an QHttp request and get the related
response, doing this turning the asynchronous operation into a synchronous one,
without using signals and slots..
Here the code:
....
int id = http->request(header, byteArray);
while(http->currentId() != 0)
{
qApp->processEvents();
}
http = new QHttp(this);
....
int id = http->request(header, byteArray);
while(http->currentId() != 0)
{
qApp->processEvents();
}
QString result = http->readAll();
To copy to clipboard, switch view to plain text mode
In my PC I get the result correctly, in other ones the Gui seems to freeze and the application don't get on..
I am wandering if the "qApp->processEvents();" instruction is the problem and how i can avoid this behaviour..
Using
while(http->currentId() != 0)
{
}
while(http->currentId() != 0)
{
QCoreApplication::processEvents();
}
To copy to clipboard, switch view to plain text mode
would be the same?
If so, what about the fallowing method ??
QNetworkAccessManager manager;
tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)),
&q, SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(
QUrl("http://www.qtcentre.org")));
tT.start(5000); // 5s timeout
q.exec();
if(tT.isActive()){
// download complete
tT.stop();
} else {
// timeout
}
QNetworkAccessManager manager;
QEventLoop q;
QTimer tT;
tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)),
&q, SLOT(quit()));
QNetworkReply *reply = manager.get(QNetworkRequest(
QUrl("http://www.qtcentre.org")));
tT.start(5000); // 5s timeout
q.exec();
if(tT.isActive()){
// download complete
tT.stop();
} else {
// timeout
}
To copy to clipboard, switch view to plain text mode
How to adapt it to perform my task (sending request and waiting for its response before going on with the flow) ???
Many thanks in advance
Roby
Bookmarks