I am in troubled situation on using QNetworkAccessManager in QThread. The same functions working fine without using threads. I am currently using only one thread and I need to add few more also.
It fires error message : "Object::connect: No such slot QThread::replyFinished(QNetworkrReply*)"
The header file code (NewThread.h ) is:
class NewThread: public QThread
{
public slots:
void replyFinished(QNetworkReply* reply);
protected:
void run();
private:
};
The source code file(NewThread.cpp ):
void NewThread::replyFinished(QNetworkReply *net_reply)
{
QByteArray data = net_reply->readAll();
QString str(data);
}
void NewThread::run()
{
QNetworkAccessManager *manager;
manager = new QNetworkAccessManager ();
QNetworkRequest req;
req.setUrl(QUrl("My url"));
QByteArray postData;
postData.append("some data string");
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
manager->setCookieJar(new QNetworkCookieJar(manager));
//Define the Request-url:
connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish (QNetworkReply *)));
//Send the request:
manager->post(req, postData);
}
void NewThread::replyFinish(QNetworkReply *reply)
{
QString answer = QString::fromUtf8(reply->readAll());
qDebug () << answer;
}
I am creating an object of NewThread class in another class, like below:
NewThread thread1;
thread1.start();
I am wondering why the same code working without threads and not with threads. I am in hard situation, any help appreciated.