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
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
To copy to clipboard, switch view to plain text mode
header:
#ifndef TRANSLATE_H
#define TRANSLATE_H
#include <QString>
#include <QtNetwork>
{
Q_OBJECT
public:
Translate();
public slots:
void dataRcd(QNetworkReply *nt);
signals:
void textTranslated
(QString *result
);
private:
QNetworkAccessManager netmanager;
};
#endif // TRANSLATE_H
#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
To copy to clipboard, switch view to plain text mode
code:
#include "translate.h"
#include <QtDebug>
Translate::Translate()
{
QObject::connect(&netmanager,
SIGNAL(finished
(QNetworkReply
*)),
this,
SLOT(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";
}
#include "translate.h"
#include <QtDebug>
Translate::Translate()
{
QObject::connect(&netmanager,SIGNAL(finished(QNetworkReply*)),this,SLOT(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";
}
To copy to clipboard, switch view to plain text mode
Any advice or hint would be a great help.
Cheers
lukas
Bookmarks