Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't stop EventLoop by signal in class
Qt 6.11 is out! See what's new in the release blog

Can't stop EventLoop by signal in class

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      1 Reply Last reply
      3
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @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 ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        S 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @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 ?

          S Offline
          S Offline
          Subuday
          wrote on last edited by
          #4

          @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;
          }
          ``
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            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.

            S 1 Reply Last reply
            3
            • mrjjM mrjj

              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.

              S Offline
              S Offline
              Subuday
              wrote on last edited by Subuday
              #6

              @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);
              }
              
              mrjjM 1 Reply Last reply
              0
              • S Subuday

                @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);
                }
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved