PDA

View Full Version : QNetworkAccessManager does not signal finished



lukas.zachy
16th November 2010, 22:44
Hello all,
I begin to play with Qt and got stuck with QNetworkAccessManager.
I can not find any difference to the provided example (googlesuggest), but the example works and my code does not :(

It seems that the network manager doesn't emit finished signal.

The project consists of two classes, one is GUI, second should should access web.

debugging output is:
Starting D:\documents\programming\slovnik-prekladac-qt\Dict-build-desktop\debug\Dict.exe...
translateText called
translateText ends
D:\documents\programming\slovnik-prekladac-qt\Dict-build-desktop\debug\Dict.exe exited with code 0

header:


#ifndef TRANSLATE_H
#define TRANSLATE_H
#include <QString>
#include <QtNetwork>
class Translate :public QObject
{
Q_OBJECT
public:
Translate();
void translateText(QString which);
public slots:
void dataRcd(QNetworkReply *nt);
signals:
void textTranslated(QString *result);

private:
QNetworkAccessManager netmanager;
};
#endif // TRANSLATE_H

code:


#include "translate.h"
#include <QtDebug>
Translate::Translate()
{
QObject::connect(&netmanager,SIGNAL(finished(QNetworkReply*)),this,S LOT(dataRcd(QNetworkReply*)));
}

void Translate::dataRcd(QNetworkReply *nt){
qDebug()<<"dataRcd called";
emit textTranslated(new QString ("slot dataRcd called"));
}

void Translate::translateText(QString which){
qDebug() <<"translateText called";
emit textTranslated(new QString("pre network manager"));
QNetworkRequest req(QUrl("http://www.google.com"));
netmanager.get(req);
qDebug()<<"translateText ends";
}


Any advice or hint would be a great help.

Cheers
lukas

tbscope
17th November 2010, 04:38
See the network reply error for possible errors.

lukas.zachy
17th November 2010, 10:17
not sure if I am doing it right...
I changed it to

void Translate::translateText(QString which){
qDebug() <<"translateText called";
emit textTranslated(new QString("pre network manager"));
QNetworkRequest req(QUrl("http://www.google.com"));
QNetworkReply *rpl = netmanager.get(req);
qDebug()<<rpl->errorString();
qDebug()<<rpl->error();
qDebug()<<"translateText ends";
}
output is now
translateText called
"Unknown error"
0
translateText ends

merajc
25th January 2011, 16:55
Hello. Apologies for posting on such an old thread, I'm new to the forum.

I've got the same exact problem, and I just can't figure out the solution. Has anyone got tips? Should I post my project here?

Thank you

ChrisW67
25th January 2011, 22:33
The example in the zip file does not compile as-is. However, the problem appears to be that the Translate object is stack allocated and goes out of scope and is destroyed before a reply can be received.

If you still cannot see the problem then post a compilable example that shows what you think is broken.

merajc
26th January 2011, 09:05
Thanks for the reply, that was exactly the problem.

I made the object heap-allocated and it did not work. I then simply added a destructor and it worked.