0

i have something really strange i have this code :

i think i know what is wrong but i dont know how to fix it . this is what i have : when i put break point in int test = 0; it getting there before it gets to httpFinished() slot in the HttpClient , mybe this is the problem ? in the main.cpp

---------------------------------------------------------------------------------------------------------
@while (i.hasNext())
{
i.next();

ThreadWorker* pThreadWorker = new ThreadWorker();
pThreadWorker->setUrl(sUrl);
QThreadPool::globalInstance()->start(pThreadWorker);
}
QThreadPool::globalInstance()->waitForDone();



---------------------------------------------------------------------------------------------------------
void ThreadWorker::run()
{
  startWork();
}

void ThreadWorker::startWork()
{
 m_pHttpClient = new HttpClient();
 m_pHttpClient->startRequest(m_url);
 int test = 0;


}

--------------------------------- HttpClient  based on the http example from Qt -----------------------------------

HttpClient::HttpClient()
{
  m_networkManager = new QNetworkAccessManager(this);
  connect(m_networkManager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
            this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

 #ifndef QT_NO_OPENSSL
    connect(m_networkManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
            this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
 #endif
}



void HttpClient::startRequest(QUrl url)
{
   m_url.setUrl("http://qt.nokia.com/");
  QNetworkRequest request; 
  request.setUrl(m_url);

 reply = m_networkManager->get(request);


 connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
         this, SLOT(slotError(QNetworkReply::NetworkError)));

 connect(reply,SIGNAL(finished()),
            this, SLOT(httpFinished()));

    connect(reply, SIGNAL(readyRead()),
            this, SLOT(httpReadyRead()));

    connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
            this, SLOT(updateDataReadProgress(qint64,qint64)));


}

the httpFinished() function that is under private slots: never triggered , why ?

UPDATED THE QUESTION

user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

1

Since the HttpClient and QNetworkAccessManager objects are created within the thread, they automatically belongs to that thread (see QObject::moveToThread), and they both needs an event loop running in that thread, for QNAM to do any work at all, and for your QObject derived class to be able to execute the slots.

You could add a call to QThread::exec() in run() to run that event loop (if you were using QThread):

void Thread::run()
{
    startWork();
    exec();
}

or create and start a QEventLoop whose quit() slot has to be connected somewhere to stop the loop (for example a finished() signal in the class HttpClient that you would emit when the work is done):

void ThreadWorker::run()
{
    startWork();
    QEventLoop loop;
    QObject::connect(m_pHttpClient, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec(); 
}

Also, since Qt 4.8, QNetworkAccessManager is multithreaded, so you might not need to use threads yourself.

alexisdm
  • 29,448
  • 6
  • 64
  • 99
  • Thank i know that in 4.8 it is mt but i need to do more work in the thread not only http req , how the thread knows its done? – user63898 Oct 21 '12 at 05:57
  • @user63898 The thread doesn't stop by itself, you need to call its slot `quit()` (or connect it to the reply `finished()` signal) to stop the event loop and the thread. – alexisdm Oct 21 '12 at 18:42
  • @user63898 Sorry, I thought you were using QThread, I just added some code to do the same thing for QThreadPool/QRunnable. But really, QThread would make more sense as you wouldn't need to stop the event loop betweeen 2 tasks on the same thread. – alexisdm Oct 21 '12 at 20:26
  • also i did it but now i have new problem : http://stackoverflow.com/questions/13002747/qt-request-always-returns-status-code-0 – user63898 Oct 21 '12 at 22:18