PDA

View Full Version : QNetworkAccesManager (Connect not emiting signal)



Sricharan
7th July 2013, 06:42
Hi Guys,

I have been trying to use QObject::connect , but the signal is not being sent.


[code]

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(url));
QObject::connect(reply,SIGNAL(finished(QNetworkRep ly*)),this,SLOT(replyFinish(QNetworkReply*)));

[\code]

[code]

void USGSDialog::replyFinish(QNetworkReply* reply)
{

if(reply->isOpen()){
QXmlInputSource input;
input.setData(reply->readAll());

QDomDocument doc;
doc.setContent(input.data());

// Get the root element
QDomElement root = doc.firstChildElement();

// Get Data
getData(root);
if(reply->isFinished())
reply->close();
}


[\code]

My class file looks like this
[code]

class USGSDialog : public QDialog
{
Q_OBJECT

public:
explicit USGSDialog(QWidget *parent = 0);
~USGSDialog();


signals:

public slots:
void replyFinish(QNetworkReply* reply);


[\code]

Can anybody help me out why the control doesn't go into the slot ? Why is the signal not being emitted ?
Thanks in Advance

ChrisW67
7th July 2013, 10:13
Have you run qmake since you declared replyFinish() as a slot?
Are you getting a warning message about the failing connect() in the console output of the program?
How have you checked that the slot is not called?
Have you looked for an error condition on the reply?

anda_skoa
7th July 2013, 13:43
Well, you obviously are not paying attention to the output of your program, nor do you check the return value of connect().

Both would have told you that the connect failed, the output would have even told you that there is no such signal QNetworkReply::finished(QNetworkReply*).

You either need to change the connect's first argument to the network access manager or the second and forth argument to the signature of the network reply's finished signal.

Cheers,
_

Sricharan
7th July 2013, 20:38
Hi chris and anda,

Now I changed the code by changing the first argument to QNEtworkaccessManager
and I am not getting any warnings from the complier




QNetworkAccessManager *manager = new QNetworkAccessManager(this);

QObject::connect(manager,SIGNAL(QNetworkAccessMana ger::finished(QNetworkReply*)),this,SLOT(replyFini sh(QNetworkReply*)));

manager->get(QNetworkRequest(url));



and I am testing if the ReplyFinsh() is executing by creating a message box to display as soon as the control goes to slot




void USGSDialog::replyFinish(QNetworkReply* reply)
{
QMessageBox* rep = new QMessageBox();
rep->setText("Inside Reply Finmsih");
rep->show();

if(reply->isOpen()){
QXmlInputSource input;
input.setData(reply->readAll());

QDomDocument doc;
doc.setContent(input.data());

// Get the root element
QDomElement root = doc.firstChildElement();

// Get Data
getData(root);
if(reply->isFinished())
reply->close();
}
if(reply->error() != QNetworkReply::NoError){
QString err = reply->errorString();
QMessageBox* error = new QMessageBox();
error->setText(err);
error->show();
}
}




Still I dont get the slot working.

ChrisW67
7th July 2013, 23:31
You still are not checking the return value from the connect() call or the the program run time output (in the Application Output pane if you are using Qt Creator). The first will tell you the connect failed, the second will tell you why.

Sricharan
8th July 2013, 03:11
Hi Chris,

I have no warning messages from QT. When I used the same code in a new project, It worked. But I was integrating the code into an existing project, Somehow its not working.

I have the same problem from this post in the forum

http://www.qtcentre.org/threads/40257-QNetworkAccessManager-problem-It-send-s-nothing

Is there anything to do with the merging of the code into an existing project.

Cheers

Sricharan
8th July 2013, 07:57
Hi I finally got it working.

I added QEventLoop




QEventLoop loop;
QNetworkAccessManager *manager = new QNetworkAccessManager(this);

QObject::connect(manager,SIGNAL(QNetworkAccessMana ger::finished(QNetworkReply*)),this,SLOT(replyFini sh(QNetworkReply*)));

manager->get(QNetworkRequest(url));

loop.exec();


I am really curious how this worked ?

ChrisW67
8th July 2013, 08:27
I have no warning messages from QT.
With your last posted code, connect() is returning false. Qt will be outputting:


Object::connect: No such signal QNetworkAccessManager::QNetworkAccessManager::fini shed(QNetworkReply*)

when you run the program. The signal is called "finished(QNetworkReply*)"


I am really curious how this worked ?
It doesn't. There is still no signal called "QNetworkAccessManager::QNetworkAccessManager::fini shed(QNetworkReply*)"

You might have changed the program's overall behaviour by adding explicit event loop but this particular connection issue has not gone away.

anda_skoa
8th July 2013, 08:51
QNetworkAccessManager, like most of Qt's I/O classes, works asynchronous, event loop based.

Usually that event loops comes from the Qt application object created in main().
If you had to add an explicit event loop you are probably missing that object or you forgot to call its exec() method or you have that code in a thread and forgot to call its exec() method, ...

And there should be no "QNetworkAccessManager::" in the SIGNAL() macro, only the name of signal and its argument types.

Cheers,
_