Why does this Qt signal never get emitted? - c++

I am trying to get data out of a web page, but the signal finished() never gets emitted!!! I know I must be doing something wrong, but can't figure out what it is.
# webservice.h
class WebService:public QObject
{
Q_OBJECT
public:
explicit WebService(QObject *parent=0);
void getRequest(const QString &urlString);
signals:
void networkError(QNetworkReply::NetworkError ne);
void finished(QNetworkReply*);
public slots:
void parseNetworkResponse(QNetworkReply* finished);
private:
QNetworkAccessManager *netMgr;
public:
QByteArray data;
};
#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
netMgr = new QNetworkAccessManager;
connect(netMgr, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseNetworkResponse(QNetworkReply*)));
}
void WebService::getRequest(const QString &urlString)
{
QUrl url(urlString);
QNetworkRequest req;
emit finished(netMgr->get(req));
}
void WebService::parseNetworkResponse(QNetworkReply *finished)
{
if (finished->error() != QNetworkReply::NoError)
{
emit networkError(finished->error());
return;
}
data = finished->readAll();
qDebug() << data;
}
data never gets assigned a value as expected.

You aren't passing the url to the QNetworkRequest you create. Try:
QNetworkRequest req(url);
inside WebService::getRequest().
As requested, here's the source modified to allow it compile and work inside QtCreator as a single main.cpp file in a console application project:
#include <QCoreApplication>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QByteArray>
#include <QString>
#include <QDebug>
//# webservice.h
class WebService:public QObject
{
Q_OBJECT
public:
explicit WebService(QObject *parent=0);
void getRequest(const QString &urlString);
signals:
void networkError(QNetworkReply::NetworkError ne);
void finished(QNetworkReply*);
public slots:
void parseNetworkResponse(QNetworkReply* finished);
private:
QNetworkAccessManager *netMgr;
public:
QByteArray data;
};
//#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
netMgr = new QNetworkAccessManager;
connect(netMgr, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseNetworkResponse(QNetworkReply*)));
}
void WebService::getRequest(const QString &urlString)
{
QUrl url(urlString);
QNetworkRequest req(url);
emit finished(netMgr->get(req));
}
void WebService::parseNetworkResponse(QNetworkReply *finished)
{
if (finished->error() != QNetworkReply::NoError)
{
qDebug() << "QNetworkReply error: " << finished->error();
emit networkError(finished->error());
return;
}
data = finished->readAll();
qDebug() << data;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
WebService web;
web.getRequest("http://www.google.com");
return a.exec();
}
#include "main.moc"
The minor modifications that were made:
added necessary headers
added a main() that called WebService::getRequest() with an appropriate URL
added #include "main.moc" to the end of the main.cpp file so qmake would "moc-ify" it properly as a single, self-contained .cpp file
made the fix mentioned above in the answer to the question
added a qDebug() output in the error case
One final thing that needed to be done was to add QT += network in the .pro file for the project so the Qt networking modules get added to the link step and to the header search path.
Update 15 Oct 2013
From your comments, it looks like you want the QNetworkAccessManager::get() call to be synchronous. I've added another version of your example program that will block in WebService::getRequest() until the request's finished signal is received. Note that this example doesn't perform much in the way of error handling, and would probably perform very badly if the netwrok request fails to complete in a timely manner. Dealing apprpriately with errors and timeouts would be necessary for anything but example or study code.
The basic idea in this example is that the signals emitted in the asynchronous Qt networking model are driven by the framework's event loop. So when a request is made, a new 'nested' event loop is created and the WebService::getRequest() function execs that loop (and stays there) until the handler of the finished signal tells the event loop to exit.
#include <QCoreApplication>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QEventLoop>
#include <QByteArray>
#include <QString>
#include <QDebug>
//# webservice.h
class WebService:public QObject
{
Q_OBJECT
public:
explicit WebService(QObject *parent=0);
void getRequest(const QString &urlString);
signals:
void networkError(QNetworkReply::NetworkError ne);
//void finished(QNetworkReply*);
public slots:
void parseNetworkResponse(QNetworkReply* finished);
private:
QNetworkAccessManager *netMgr;
QEventLoop request_event_loop;
public:
QByteArray data;
};
//#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
netMgr = new QNetworkAccessManager;
connect(netMgr, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseNetworkResponse(QNetworkReply*)));
}
void WebService::getRequest(const QString &urlString)
{
QUrl url(urlString);
QNetworkRequest req(url);
netMgr->get(req);
request_event_loop.exec(); // wait here until the WebService::parseNetworkResponse() slot runs
// emit finished(netMgr->get(req));
}
void WebService::parseNetworkResponse(QNetworkReply *finished)
{
qDebug() << "enter parseNetworkResponse()";
if (finished->error() != QNetworkReply::NoError)
{
qDebug() << "QNetworkReply error: " << finished->error();
emit networkError(finished->error());
}
else {
data = finished->readAll();
qDebug() << data;
}
qDebug() << "request_event_loop.exit()";
request_event_loop.exit();
qDebug() << "exit parseNetworkResponse()";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
WebService web;
qDebug() << "main() getRequest()";
web.getRequest("http://www.stackoverflow.com");
qDebug() << "main() getRequest() completed";
return a.exec();
}
#include "main.moc"

Related

C++ Multi-Client TCP Server with QList

Necessary informations:
QList<QTcpSocket*> list;
QTcpServer* server;
QTcpSocket* socket;
In Qt I have built a TCP-Server(QTcpServer)! I have a QList with all my connected clients and I want to read the incomming data for each client personally. So if the QTcpServer gets a new connection, I handel it like this:
void Server::newConnection()
{
qDebug() << "New Connection";
list.append(server->nextPendingConnection());
connect(list.last(),SIGNAL(readyRead()),this,SLOT(readyRead()));
}
How can I get the correct client (out of my QList), which send the SIGNAL readyRead(), in my SLOT readyRead()?
void Server::readyRead(){
//??
}
Any help is welcomed!
Have you tried QObject::sender()? It should return the instance of the QObject which actually sent the signal. Hope that will help.
The solution:
void Server::readyRead(){
QByteArray buffer;
QTcpSocket* readSocket = qobject_cast<QTcpSocket*>(sender());
buffer = readSocket->readAll();
QString mytext = QString::fromStdString(buffer);
qDebug() << mytext;
}
This could be solved by the QSignalMapper. Here is the (not completelly tested) code:
---------------------- main.cpp ------------------
#include "rootwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RootWindow w;
w.show();
return a.exec();
}
---------------------- rootwindow.h ------------------------
#ifndef ROOTWINDOW_H
#define ROOTWINDOW_H
#include <QMainWindow>
#include <QtDebug>
#include <QLocalServer>
#include <QLocalSocket>
#include <QSignalMapper>
#include <QList>
class RootWindow : public QMainWindow
{
Q_OBJECT
private:
QLocalServer *server;
QLocalSocket *socket;
QList<QLocalSocket*> *list;
QSignalMapper *mapper;
public:
RootWindow(QWidget *parent = 0);
~RootWindow();
private slots:
void slotNewConnection();
void slotReadyRead(int index);
};
#endif // ROOTWINDOW_H
------------------------ rootwindow.cpp -------------------------
#include "rootwindow.h"
RootWindow::RootWindow(QWidget *parent): QMainWindow(parent)
{
server = new QLocalServer;
list = new QList<QLocalSocket*>;
mapper = new QSignalMapper(this);
connect(server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
connect(mapper, SIGNAL(mapped(int)), this, SLOT(slotReadyRead(int)));
server->listen("TestServer");
}
RootWindow::~RootWindow()
{
delete list;
}
void RootWindow::slotNewConnection()
{
qWarning() << "newConnection";
list->append(server->nextPendingConnection());
//here you map each client to its number in the list
mapper->setMapping(list->last(), list->length()-1);
//here we say, that when ever a client from the QList sends readyRead() the mapper should be used
//with the property (list->length()-1) defined in the line above
connect(list->last(), SIGNAL(readyRead()), mapper, SLOT(map()));
}
void RootWindow::slotReadyRead(int index)
{
qWarning() << "Client " << index << " has written: " << list->at(index)->readAll();
}
It's basically your code, I've only added the QSignalMapper and some comments at the relevant lines.

Qt QNetworkAccessManager and Slot not working

i am a beginner in c++ and i am trying to retrieve data from a website using http request and to download the data in a file .
I am using the classes :
QMainWindow
QtNetwork/QNetworkAccessManager
QtNetwork/QNetworkRequest
QtNetwork/QNetworkReply
QUrl
The thing is that the file is created but there is no data in the file and i am getting an error that i don't understand . I searched through the forum found some similar kind of problems but did not understand as i am a beginner . Please help me its a school project and i am really stuck here.
Here is the httpWindow.h code
#ifndef HTTPWINDOW_H
#define HTTPWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QUrl>
#include <QString>
class QFile;
namespace Ui {
class httpWindow;
}
class httpWindow : public QMainWindow
{
Q_OBJECT
public:
explicit httpWindow(QWidget *parent = 0);
~httpWindow();
void request(QUrl url);
private slots:
void downloadFile();
void httpFinished();
void httpReadyRead();
private:
Ui::httpWindow *ui;
QUrl url;
QNetworkAccessManager *manager;
QNetworkRequest requete;
QNetworkReply *reply;
QFile *file;
int httpGetId;
bool httpRequestAborted;
};
#endif // HTTPWINDOW_H
Here is the httpwindow.cpp
#include <QtWidgets>
#include <qnetwork.h>
#include <QString>
#include "httpwindow.h"
#include "ui_httpwindow.h"
httpWindow::httpWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::httpWindow)
{
ui->setupUi(this);
downloadFile();
}
httpWindow::~httpWindow()
{
delete ui;
}
void httpWindow::request(QUrl url)
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished()),
this, SLOT(httpFinished()));
//requete.setUrl(QUrl("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"));
requete.setUrl(url);
reply = manager->get(requete);
connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
}
void httpWindow::downloadFile()
{
QMessageBox msg ;
QUrl url("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide") ;
qDebug() << url.toString();
QFileInfo fileInfo(url.path());
//msg.setText("fileInfo = " + fileInfo);
QString fileName = "C:\\testQt\\" + fileInfo.fileName();
msg.setText("fileName = " + fileName);
if (fileName.isEmpty()){
fileName = "C:\testQt\fichier.html";
msg.setText(" création d'un nouveau fichier fichier.html ");
}
if (QFile::exists(fileName)) {
QFile::remove(fileName);
return;
}
file = new QFile(fileName);
msg.setText(" QFile::exists(fileName) == true , file : ");
if (!file->open(QIODevice::WriteOnly)) {
delete file;
file = 0;
return;
}
// schedule the request
httpRequestAborted = false;
request(url);
}
void httpWindow::httpFinished()
{
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
return;
}
file->flush();
file->close();
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error()) {
file->remove();
// QMessageBox::information(this, tr("HTTP"),
// tr("Download failed: %1.")
// .arg(reply->errorString()));
// downloadButton->setEnabled(true);
} else if (!redirectionTarget.isNull()) {
QUrl newUrl = url.resolved(redirectionTarget.toUrl());
// if (QMessageBox::question(this, tr("HTTP"),
// tr("Redirect to %1 ?").arg(newUrl.toString()),
// QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
url = newUrl;
reply->deleteLater();
file->open(QIODevice::WriteOnly);
file->resize(0);
request(url);
return;
}
reply->deleteLater();
reply = 0;
delete file;
file = 0;
}
void httpWindow::httpReadyRead()
{
// this slot gets called every time the QNetworkReply has new data.
// We read all of its new data and write it into the file.
// That way we use less RAM than when reading it at the finished()
// signal of the QNetworkReply
if (file)
file->write(reply->readAll());
}
Here is the main.cpp code
#include "httpwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
httpWindow w;
w.show();
return a.exec();
}
The errors :
can't find linker symbol for virtual table for `QMessageBox' value
found `RGB_MASK' instead
can't find linker symbol for virtual table for `QMessageBox' value
found `RGB_MASK' instead
"http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"
QObject::connect: No such signal QNetworkAccessManager::finished() in ..\ppe3_trading_test\httpwindow.cpp:24
QObject::connect: (receiver name: 'httpWindow')
QObject::connect: No such signal QNetworkReplyHttpImpl::&reply::readyRead() in ..\ppe3_trading_test\httpwindow.cpp:31
QObject::connect: (receiver name: 'httpWindow')
Please do help me its really important for my schooling .
connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
You're mixing up old syntax and new syntax, it should be
connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
or better yet using new syntax(Qt5 only):
connect(reply, &QNetworkReply::readyRead, this, &httpWindow::httpReadyRead);
QNetworkAccessManager doesn't have a finished() signal it has a finished(QNetworkReply*) signal, read the docs.

Delete QNetworkReply *reply cause SIGSEGV outside of the code?

I wrote a simple program to automate the process setting the router. After a check, I find a pointer need to be delete (QNetworkReply *reply in replyFinish())and do the job, but after that the program crash and Qt show it stopped in some assembly code :(
So my questions are:
Are there any common practices to handle situation like that (Qt
show the program stopped in some assembly code)
What did I do wrong when delete a pointer which isn't used anymore?
(pretty sure about that)
Here is the code:
#ifndef HTTPGETTER_H
#define HTTPGETTER_H
class QNetworkAccessManager;
class QNetworkReply;
class QNetworkRequest;
class QAuthenticator;
#include <QObject>
class httpGetter : public QObject
{
Q_OBJECT
private:
QNetworkAccessManager *nam;
public:
explicit httpGetter(QObject *parent=0);
~httpGetter();
public slots:
void replyFinish(QNetworkReply* reply);
void onAuthen(QNetworkReply*,QAuthenticator*);
};
implements
#include "httpgetter.h"
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <QAuthenticator>
#include <QDebug>
httpGetter::httpGetter(QObject *parent) : QObject(parent)
{
nam = new QNetworkAccessManager(this);
QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
QObject::connect(nam,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),this,SLOT(onAuthen(QNetworkReply*,QAuthenticator*)));
QUrl url("http://192.168.1.1");
QNetworkReply *reply= nam->get(QNetworkRequest(url));
}
httpGetter::~httpGetter(){
delete nam;
}
void httpGetter::replyFinish(QNetworkReply *reply)
{
if (reply==NULL) {
qDebug() << "NULL reply";
return;
}
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug() << statusCode.toString();
if (reply->error()==QNetworkReply::NoError) {
qDebug() << "NICE reply";
QByteArray bytes = reply->readAll();
QString answer = QString(bytes);
qDebug()<< answer;
}
else {
qDebug() << "reply error";
}
//delete reply; ==> delele cause sigsegv, if don't the program run like a while(true)
//reply == NULL;
}
void httpGetter::onAuthen(QNetworkReply* rep,QAuthenticator* auth)
{
if ( rep==NULL || auth == NULL) {
qDebug()<< "reply or authentication pointer is null";
return;
}
qDebug()<< rep->readAll();
auth->setUser("rolan");
auth->setPassword("123456");
}
main
#include <QtCore/QCoreApplication>
#include "httpgetter.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
httpGetter abc;
return a.exec();
}
According to the documentation:
Note: After the request has finished, it is the responsibility of the
user to delete the QNetworkReply object at an appropriate time. Do not
directly delete it inside the slot connected to finished(). You can
use the deleteLater() function.
Also, since
nam = new QNetworkAccessManager(this);
creates a new QNetworkAccessManager with this as its parent, and QObject's destructor will automatically delete all of its children, your delete nam; in httpGetter's destructor is unnecessary and will end up causing a double delete.

basic QNetworkAccessManager fails to send requestion

There is no error in my code though there is never any request packet hits my server. I run tcpdump port 80 and nothing ever crosses the wire, wireshark says the same.
//coreeng.h
#ifndef COREENG_H
#define COREENG_H
#include <QObject>
#include <QNetworkAccessManager>
class coreEng : public QObject
{
Q_OBJECT
public:
private slots:
public slots:
void connect();
void url();
void finishedSlot(QNetworkReply* reply);
private:
QNetworkAccessManager* nam;
};
#endif // COREENG_H
//coreengine.cpp
#include "coreeng.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
coreEng::coreEng(QObject *parent) :
QObject(parent)
{
}
explicit coreEng(QObject *parent = 0)
{
nam = new QNetworkAccessManager();
}
void coreEng::connect(){
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
void coreEng::url(){
QUrl url("http://www.myserver.com");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void coreEng::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}
else
{
}
delete reply();
}
//main.cpp
#include <QCoreApplication>
#include "coreeng.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
coreEng mConnect;
mConnect.connect();
return a.exec();
}
You never call coreEng::url() so you never make a request.
I'm also not sure your constructors are correct. Why not just ditch the explicit constructor and create the QNetworkAccessManager in the default constructor?

How can I download data from any link?

I want to get string that appears on following link in my Qt program how can I get it ?
http://en.wikipedia.org/w/api.php?action=opensearch&search=centaurus_constellation
I have coded following files to get string appearing on above page in m_DownloadedData, but it is storing empty string at the end of the program , can someone please help me get data from above link ?
.h file:
#ifndef SKYOBJDESCRIPTION_H
#define SKYOBJDESCRIPTION_H
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class SkyObjDescription : public QObject
{
Q_OBJECT
public:
explicit SkyObjDescription(const QString soName, const QString soType, QObject* parent = 0);
virtual ~SkyObjDescription();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply* reply);
private:
QString soName,soType;
QByteArray m_DownloadedData;
};
#endif // SKYOBJDESCRIPTION_H
and
.cpp file
#include <QString>
#include <QUrl>
#include <QDebug>
#include "skyobjdescription.h"
SkyObjDescription::SkyObjDescription(const QString so_Name, const QString so_Type, QObject* parent): soName(so_Name), soType(so_Type), QObject(parent)
{
QString wikiLink = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+ soName.replace(" ", "_").toLower() + "_" + soType.toLower() + "&format=xml&limit=1.xml";
// QUrl wikiUrl("http://en.wikipedia.org/w/api.php?action=opensearch&search=hello_world&format=xml&limit=1.xml");
m_DownloadedData = wikiUrl.toEncoded();
qDebug() << "wikiUrl.toEncoded(): " << m_DownloadedData;
QNetworkRequest req(wikiUrl);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)));
manager->get(req);
}
SkyObjDescription::~SkyObjDescription()
{
}
void SkyObjDescription::fileDownloaded(QNetworkReply* reply)
{
m_DownloadedData = reply->readAll();
qDebug() << "received reply";
qDebug() << m_DownloadedData;
reply->deleteLater();
emit downloaded();
}
QByteArray SkyObjDescription::downloadedData() const
{
qDebug() << m_DownloadedData;
return m_DownloadedData;
}
part of main.cpp
SkyObjDescription * skd = new SkyObjDescription(m_Name, "Constellation");
QString data(skd->downloadedData());
qDebug() << data;
delete skd;
If there is other way to get data from link please explain it.
Thank you very much :)
What you probably experiment is caused by your double call on QNetworkReply::readAll.
Remembers it is a IO operation, and there is no way to read multiple times the information contain by the netwrk reply.
Just comment your debug line :
void SkyObjDescription::fileDownloaded(QNetworkReply* pReply)
{
//qDebug() << pReply->readAll();
m_DownloadedData = pReply->readAll();
//emit a signal
pReply->deleteLater();
emit downloaded();
}
edit (for completness)
The test code I use :
#ifndef TMP_H
#define TMP_H
#include <QObject>
class QNetworkReply;
class Tmp : public QObject
{
Q_OBJECT
public:
explicit Tmp(QObject *parent = 0);
signals:
public slots:
void displayResult(QNetworkReply* reply);
};
#endif // TMP_H
//tmp.cpp
#include "tmp.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QDebug>
Tmp::Tmp(QObject *parent) :
QObject(parent)
{
QUrl url("http://en.wikipedia.org/wiki/Centaurus_constellation");
QNetworkRequest req(url);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(displayResult(QNetworkReply*)));
manager->get(req);
}
void Tmp::displayResult(QNetworkReply *reply) {
QByteArray buffer = reply->readAll();
qDebug() << "received reply";
qDebug() << buffer;
reply->deleteLater();
}