PDA

View Full Version : QNetworkAccessManager::get() problem



batileon
16th January 2012, 04:21
http://www.qtcentre.org/threads/40257-QNetworkAccessManager-problem-It-send-s-nothing

I am having a similar problem here.
I used the QNetworkAccessManager::get()
But from server side, I found no http request has been sent actually.



void Report::test()
{
QNetworkAccessManager * manager = new QNetworkAccessManager();
QNetworkConfigurationManager config_manager;
manager->setConfiguration(config_manager.defaultConfigurati on());
connect( manager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( webReportGenerateFinished(QNetworkReply*) ));
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://192.168.0.199/webreport/shift.php?title=ShiftReport&fromdate=2012-01-03&todate=2012-01-03&shift[]=2&emp[]=1&stationNo=1&lang=1&format=1&rounddp=1")));
}

void Report::webReportGenerateFinished(QNetworkReply* reply)
{
cout<<"RM generate finished!"<<endl;
qDebug("RM generate finished!");
}


Header file:

class Report : public QObject
{
Q_OBJECT

public :
...
...
...
...

public slots:
void webReportGenerateFinished(QNetworkReply * reply);



However, from http access log, NO request has been received.
Would anyone please tell me what's going on?

ChrisW67
16th January 2012, 04:52
Are you returning to the Qt event loop after issuing your request?

Is the finished slot being called? Have you looked at the error return available in the QNetworkReply? It might give you a clue.

batileon
16th January 2012, 04:55
um...firstly I have not much idea for the event loop
there's no error returned since I guess..... the request didn't send actually.

ChrisW67
16th January 2012, 08:13
So the slot is called but the error is QNewtorkReply::NoError or the slot is not called? These are not the same thing.

The event loop in a typical Qt program is provided by the app.exec() call in your main() function. If there is no Qt event loop, or you are keeping the program from returning to it (e.g. by busy waiting or calling some blocking process) then the network request will not be sent and your GUI will be unresponsive. For example, if you did this:


Report rep;
...
rep.test();
while (the result of the finished request is not present)
waitAWhile();

then your code will never leave the while loop because the request cannot be sent until control returns to the event loop (i.e. your program becomes idle).

Put together a small, self-contained example program that displays the problem you are seeing.

batileon
16th January 2012, 08:27
Thanks!

The case is actually slot not called, and from server I found the http request not even being sent.

I tried to add a few lines involving QEventLoop, after calling the exec(), it worked!:)

But I am still figuring how to use the eventloop since .....it holds the program, I am finding way to end it........

ChrisW67
16th January 2012, 09:07
Call QEventLoop::quit() or exit(). However, you should reconsider the design that is causing the program to block and not return to the main Qt event loop in the first place.

batileon
16th January 2012, 09:26
I am a bit confused.
How can I call the quit() or exit without the object? Where to quit? Inside the slot?

What I have now is something like:

void Report::test()
{
QEventLoop loop;

QNetworkAccessManager * manager = new QNetworkAccessManager();
QNetworkConfigurationManager config_manager;
manager->setConfiguration(config_manager.defaultConfigurati on());
connect( manager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( webReportGenerateFinished(QNetworkReply*) ));
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://192.168.0.199/webreport/shift.php?title=ShiftReport&fromdate=2012-01-03&todate=2012-01-03&shift[]=2&emp[]=1&stationNo=1&lang=1&format=1&rounddp=1")));

loop.exec()
}

void Report::webReportGenerateFinished(QNetworkReply* reply)
{
cout<<"RM generate finished!"<<endl;
..
..
..// call loop.quit() here??? Do not have the loop object actually....

qDebug("RM generate finished!");
}

ChrisW67
17th January 2012, 00:37
QEventLoop::quit() is a slot... connect something to it, or change the scope of the QEventLoop.

Better still, identify the flaw in your original design. What are you doing after you call test() that is not allowing the normal flow of events?