[SOLVED] moveToThread and QEventLoop
-
I have made a class which queries data from the net. I have used QEventLoop to wait until QNetworkReply is finished. And it works greatly. Here is example.
@
void myclass::queryData()
{
QNetworkAccessManager *m_manager = new QNetworkAccessManager();
QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));QEventLoop loop;
connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec();QByteArray b = reply->readAll();
// .... etc.
}
@But now I should use this class with multithreading. So I think to use moveToThread
@
QThread *thread = new QThread();
myclass *mc = new myclass();
mc->moveToThread(thread);
connect(thread,&QThread::started,mc,&myclass::queryData);
thread->start();
@But now QEventLoop seems to be problem. It doesn't continue executing. So how to solve this problem? I wouldn't like to use signal/slot to wait QNetworkReply because I have many consecutive queries in the same class.
-
You need to assign a parent to QThread object, for example:
@
QThread *thread = new QThread(this);
@In general, it should work, even in a thread. Although, of course, using signals and slots would be better.
-
Unfortunately, it didn't help.
EDIT: hmmm... the same problem seems to be even signal and slot is used. Why QNetworkReply::finished signal not emitted when using moveToThread?
-
Why do you want to have eventLoop ? You are making the program to wait since you are doing exec. I presume that you want to wait for reading till download finished. If that is the case, you can just work with readyRead, downloadProgress or finished signals.
-
It's not immediately obvious what the problem is, but I suspect that some of the member variables in myclass were not moved to the other thread, because you didn't give them a parent.
See the documentation on Thread Affinity: http://qt-project.org/doc/qt-5/QObject.html#thread-affinity
"A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called."
Just to check, do you see any warning messages at the console output?
[quote author="jimcad" date="1408001067"]
@
QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));QEventLoop loop;
connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec();QByteArray b = reply->readAll();
// .... etc.
@
[/quote]By the way, you don't need an extra event loop. You can use a lambda instead:@
QNetworkReply *reply = m_manager->get(QNetworkRequest( QUrl( "http://url...." )));connect(reply,&QNetworkReply::finished, [=]
{
QByteArray b = reply->readAll();
reply->deleteLater();
// .... etc.
}
@ -
Thanks. Indeed the problem must be one of member variable. I created a new test class and it seems to work now.
And thanks for the tip about lambda slot.