Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I`ve written a method to send a request and store data in a QString, but it seems that the signal is never emitted because the program stops at the point of waiting for signal.

Can you check my code and see if I`m wrong or something? I`ve tested it with different URLs (http and https) and 2 different networks so that shouldn`t be a problem.

P.S: I should also mention that while building (I use qmake) I get a warning:

warning: ‘manager’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     manager->get (QNetworkRequest (QUrl (url)));


What I have tried:

void ReqProcessor::getUpdates () //send request
{
    QString url = baseURL;
    QNetworkAccessManager *manager = new QNetworkAccessManager;
    QNetworkReply *reply;
    url.append ("getupdates?timeout=100");
    qDebug() << url; //Debug
    reply = manager->get (QNetworkRequest (QUrl (url)));
    connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished()));
    qDebug() << reply->error(); //debug
    while (!ready)
    {
        continue;
    }
    updates = reply->readAll ();
    qDebug() << updates; //Debug
}


void ReqProcessor::replyFinished ()
{
    qDebug() << "Ready";
    ready = 1;
}
Posted
Updated 15-Jul-17 21:14pm

The finished signal and the replyFinished slot have different signatures. Try to define your slot with the same signature as your signal. Something like:

C++
void ReqProcessor::replyFinished (QNetworkReply* reply)
{
    //...
}

The default Qt connection type is AutoConnection which means that if the emitted signal is from another thread, it's slot is queued and invoked when control returns to the current thread's event loop (like a QueuedConnection). Since you have a blocking loop in your thread, the control is never returns to the thread's event loop...

Try to connect your slot with a DirectConnection. Something like:

C++
connect (manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished()), Qt::DirectConnection);
 
Share this answer
 
v2
Comments
Ali-RNT 16-Jul-17 9:40am    
Thanks for your answer, but it`s not working. Someone suggested moving that connection to before calling get() methode, but it doesn`t work, too.
Shmuel Zang 17-Jul-17 3:30am    
Yes. It seems like the slot's signature isn't the problem (a slot can take fewer arguments than its connected signal). See the improved solution.
Ali-RNT 18-Jul-17 0:38am    
Still didn`t work, but I figured it out! I should have used a QEventLoop. next answer tells how. Thanks for your suggestions.
Shmuel Zang 18-Jul-17 5:18am    
Well, it's hard to guess that you had a problem with the lacking of a QApplication or QCoreApplication (that should be in another function), without seeing the entire code. Anyway, it seems like you still needed to use a DirectConnection...
It finally got to work using a QEventLoop. here is the improved code (and that slot is removed, since it`s no more neccesary)

C++
void ReqProcessor::getUpdates ()
{
    QString url = baseURL;
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QNetworkReply *reply;
    QEventLoop loop;
    url.append ("getupdates?timeout=100");
    connect (manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()), Qt::DirectConnection);
    reply = manager->get (QNetworkRequest (QUrl (url)));
    loop.exec();
    updates = reply->readAll ();
}


And aother mistake was, my program was lacking a QApplication or QCoreApplication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900