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;
    }
    

  • Lifetime Qt Champion

    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.


  • Moderators

    @Subuday
    @mrjj is right of course,

    But I have the feelings, from what you showed so far, that you'll end up in trouble later down the road.

    Can you tell us, what exactly you want to archive ?



  • @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;
    }
    ``

  • Lifetime Qt Champion

    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);
    }
    

  • Lifetime Qt Champion

    @Subuday
    Well your small sample works so that must mean you are doing something slightly different.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.