user63898
user63898

Reputation: 30885

Qt request never trigger the finished() signal

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

Upvotes: 0

Views: 1091

Answers (1)

alexisdm
alexisdm

Reputation: 29886

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.

Upvotes: 1

Related Questions