PDA

View Full Version : web server connection



sattu
2nd March 2011, 14:13
Hi everyone,
I am trying to connect to web server.For that i have 3 function(A,B,X).

code of A :-(for sending request)


mang= new QNetworkAccessManager();
connect(mang, SIGNAL(finished(QNetworkReply*)),this,SLOT(B(QNetw orkReply* )));
QNetworkRequest request(QUrl("http://192.168.1.95/test/default.aspx"));
mang->post(request,data);


code of B ;- (reply of server)


B( QNetworkReply* reply)
{
data=reply->readAll();
}



X is the function where i am trying to call A.

But problem is the data which i received from server is required inside X (means i want to use that data inside X). But i received the data after X function.

Is there any way so that i can use the data in side X.

thanks.

unit
2nd March 2011, 15:37
You can use blocked mode (from QIODevice::waitForReadyRead), but best way of code is use two function Y and Z. Where Y is first part of X. And Z is second (Also you can use signals|and slot). Example of code that i'm use:

A (called from Y):


reply = manager.get(QNetworkRequest(apiUrl));
connect(reply, SIGNAL(finished()),this, SLOT(on_replyfinish()));
connect(reply, SIGNAL(readyRead()), this, SLOT(on_read()));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), reply, SLOT(ignoreSslErrors()));

on_read():


void MainWindow::on_read()
{
result+=reply->readAll();
}

on_replyfinish():


void MainWindow::on_replyfinish()
{//oh, called on error or when all data is readed.
if(reply->error()!=QNetworkReply::NoError)
{
//error handle
}
else
{
Z();
}

}

sattu
3rd March 2011, 11:44
thanks for reply.Now it is working.