Can't stop EventLoop by signal in class
-
I emit signal but EventLoop still continue work.
class Client : public QObject { Q_OBJECT public: explicit Client(QObject *parent = nullptr); int result; void getFinish(); int getResult(); signals: void finishStatus(); };
int Client::getResult() { QEventLoop loop; connect(this, SIGNAL(finishStatus()), &loop, SLOT(quit())); getFinish(); loop.exec(); return result; } void Client::getFinish() { result = 2 + 3; emit finishStatus(); }
Also I try call getFinish() above EventLoop:
int Client::getResult() { getFinish(); QEventLoop loop; connect(this, SIGNAL(finishStatus()), &loop, SLOT(quit())); loop.exec(); return result; }
-
Hi
the emit is like a direct function call so it has happened when code
reaches loop.exec();
Use
connect(this, SIGNAL(finishStatus()), &loop, SLOT(quit()), Qt::QueuedConnection);
and it should work. -
@J.Hilk I need wait for data from Http Request but the solution doesn't help.
My code:void Client::getProjTypesRequest(const int &id) { QString uri = "projectTypeList?option=%1"; QNetworkRequest request(url + uri.arg(id)); QNetworkReply *reply = manager->get(request); connect( reply, SIGNAL(finished()),this, SLOT(authenticationReply())); } void Client::getProjTypesReply() { QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender()); qDebug() << "getProjTypesReply"; if (reply->error() == QNetworkReply::NoError) { qDebug() << "getProjTypesReply: OK!"; QByteArray httpReply = reply->readAll(); qDebug() << httpReply; QJsonDocument jsonResponse = QJsonDocument::fromJson(httpReply); QJsonArray jsonArray = jsonResponse.array(); foreach(const QJsonValue & value, jsonArray) { QJsonObject obj = value.toObject(); QMap<QString, QString> projectType; projectType.insert("id", obj["id"].toString()); projectType.insert("name", obj["name"].toString()); projectTypeList.append(projectType); } emit getProjectTypesStatus(); manager->clearAccessCache(); } else { qDebug() << reply->errorString(); emit getProjectTypesStatus(); manager->clearAccessCache(); } reply->deleteLater(); } QList<QMap<QString, QString> > Client::getProjectTypeList(const int &projectField) { QEventLoop loop; getProjTypesRequest(projectField); connect(this, SIGNAL(getProjectTypesStatus()), &loop, SLOT(quit()), Qt::QueuedConnection); loop.exec(); qDebug() << "projectTypeList Length:"; qDebug() << projectTypeList.length(); return projectTypeList; } ``
-
Hi
Try flip the lines (connect first)
getProjTypesRequest(projectField);
connect(this, SIGNAL(getProjectTypesStatus()), &loop, SLOT(quit()), Qt::QueuedConnection);Anyway, why do you need to wait?
If you just use the normal async API, you are called when data is read and
when ready, you can process data and emit a signal to tell other objects that task is done. -
@mrjj I get data from my server for the list Model.
EDIT
flips line doesn't help!
I gets qDebug() << "projectTypeList Length:" ; only when I close programm.void TypeProjectListModel::load(const int &projectField) { listData = client->getProjectTypeList(projectField); }