I'm having trouble collecting the response from a web request i do. (Because i'm new to Qt).
Why do i have trouble?
I have a request class which send a request and receive a response. But i can't get the response to the parent of the request object, because i have to wait for a "finished" signal from the NetworkAccessMaanager which handles the response.
So i handle the response in a "finished" slot, but i can't return the info to the parent main window holding the request object. How can i do this?
Here's the code:
Main window.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_buttonLogin_clicked()
{
request->sendRequest("www.request.com");
}
Request.cpp:
Request::Request()
{
oNetworkAccessManager = new QNetworkAccessManager(this);
QObject::connect(oNetworkAccessManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
}
/*
* Sends a request
*/
QNetworkReply* Reques开发者_JAVA技巧t::sendRequest(QString url)
{
QUrl httpRequest(url);
QNetworkRequest request;
request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // Set default ssl config
request.setUrl(httpRequest); // Set the url
QNetworkReply *reply = oNetworkAccessManager->get(QNetworkRequest(httpRequest));
return reply;
}
/*
* Runs when the request is finished and has received a response
*/
void Request::finishedSlot(QNetworkReply *reply)
{
// Reading attributes of the reply
// e.g. the HTTP status code
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (reply->error() == QNetworkReply::NoError)
{
// Reading the data from the response
QByteArray bytes = reply->readAll();
QString jsonString(bytes); // string
bool ok;
QVariantMap jsonResult = Json::parse(jsonString,ok).toMap();
if(!ok)
{
qFatal("An error occured during parsing");
exit(1);
}
// Set the jsonResult
setJsonResult(jsonResult);
}
// Some http error received
else
{
// handle errors here
}
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete reply;
}
/*
* Set the json result so that other functions can get it
*/
void Request::setJsonResult(QVariantMap jsonResult)
{
m_jsonResult = jsonResult;
}
/*
* Get the json result
* Return null if there is no result
*/
QVariantMap Request::getJsonResult()
{
return m_jsonResult;
}
Any ideas of how i can do this?
Thanks in advance!
Each QNetworkReply
emits finished()
signal, so you should connect signal from QNetworkReply*
returned by request->sendRequest("www.request.com");
to slot of MainWindow
.
EXAMPLE:
void MainWindow::on_buttonLogin_clicked()
{
QNetworkReply *reply = request->sendRequest("www.request.com");
connect(reply, SIGNAL(finished()), this, SLOT(newslot()));
}
void MainWindow::newslot()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
// there you can handle reply as you wish
}
精彩评论