0

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.

Zain
  • 1
  • 2
  • You need a `Q_OBJECT` macro in each class that defines slots & signals. – Mat Oct 18 '12 at 13:58
  • Q_OBJECT is not working, I have tried it already..It fires a bunch of other errors. newthread.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall NewThread::metaObject(void)const " (?metaObject@NewThread@@UBEPBUQMetaObject@@XZ) – Zain Oct 18 '12 at 14:01
  • Then you need to fix those errors. That macro is **required**. – Mat Oct 18 '12 at 14:01
  • any soggestions for the error.newthread.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall NewThread::metaObject(void)const " – Zain Oct 18 '12 at 14:12
  • Clean build and make sure whatever build system you're using is calling `moc` on your headers. – Mat Oct 18 '12 at 14:15

3 Answers3

0

Don't forget that if the signal is coming from a separate thread you need to start the event loop of your thread that catches the signal. Read the detailed description of QThread. And don't forget the Q_OBJECT macro.

Furkan
  • 683
  • 2
  • 10
  • 26
  • You do not create an event loop. You should just start the event loop of your thread. You do this by calling the exec method of the QThread. – Furkan Oct 19 '12 at 05:50
0

You need to add the Q_OBJECT macro in your class declaration

Then you need to preprocess the header file with Qt's "moc" tool and add the generated cpp file to your project.

Once you build everything together it should work properly

alberthier
  • 643
  • 1
  • 7
  • 9
  • I did exploration for it and it says that qmake does it automatically(moc)..how will I do it manually..I am on win7 with Qt creator 4.8.x .. plz help I am newbie with qt. – Zain Oct 18 '12 at 15:05
0

In your header file, you declared replyFinished

public slots:
         void replyFinished(QNetworkReply* reply);

But, in your source file, you connect to replyFinish

connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish (QNetworkReply  *)));

The QObject::connect cannot find replyFinish because it is just a method, not slot. Please connect to correct slot name.

As the other people say, you need Q_OBJECT.

Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38