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. Wait till POST request finished
Forum Updated to NodeBB v4.3 + New Features

Wait till POST request finished

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.6k Views
  • 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.
  • A Offline
    A Offline
    Andre.Muller
    wrote on last edited by
    #1

    My program is getting data from mysql table via json format through php script, that generating json.

    My function sends to server post request (with some params) and getting response. With that all okay. Works fine. But, when I want to get specified cell (data from json array), it takes empty string (program works fast and taking data from a string before it sets up)

    Here's the code with post request:

    void dbase::requestor(QString option)
    {
        curr_js = ""; //nulling string
        QString urla = host+"transfer.php";
        // /////////
        QNetworkAccessManager * manager = new QNetworkAccessManager(this);
        QUrl url(urla);
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        QUrlQuery params;
        params.addQueryItem("api", key);
        params.addQueryItem("option", option);
        //QEventLoop loop;
        connect(manager, SIGNAL(finished(QNetworkReply *)),this, SLOT(replyFinished(QNetworkReply *)));
        //loop.exec(); tryed eventloop, but bcuz of this my replyFinished is not accessible. error code under this one
        manager->post(request, params.query().toUtf8());
    
    }
    

    If using eventloop

    QObject::connect: No such slot QEventLoop::replyFinished(QNetworkReply *)
    

    replyFinished function (setting up variable)

    void dbase::replyFinished(QNetworkReply *reply)
    {
        curr_js = reply->readAll();
    }
    

    Usage (where is problem)

    QString req = "SOME REQUEST";
        database.requestor(req);
        if (database.db_cell (0,0) == "")
        {
            qDebug()<<database.db_cell (0,0) << " - EMPTY - "<<database.curr_js;
        }
    

    So in this case I'm getting

    ""  - EMPTY -  ""
    

    But if I getting data from string (created button for test), it's there:

    {"0":["1", "Admin","hashpwd"],"1":["2", "Albert","hashpwd"]}
    

    db_cell function

    QString dbase::db_cell (int indexrow, int indexcols)
    {
        QJsonDocument sd = QJsonDocument::fromJson(curr_js.toUtf8());
        QJsonObject setobj = sd.object();
        QJsonValue qqq = setobj.value(QString(QString::number(indexrow))).toArray()[indexcols];
        return qqq.toString();
    }
    

    As I see, problem is that program need to wait before getting a data from json-string.

    1 Reply Last reply
    0
    • A Andre.Muller

      @Christian-Ehrlicher send signal? but how prevent code running while it finishes? can You provide some examples? just didn't get how to make it working.

      and, btw, I thought, good option will be eventloop. I mean, it will pause running while data is getting.

      Here's sample:

      QEventLoop loop;
         manager->post(request, params.query().toUtf8());
         connect(manager, SIGNAL(finished(QNetworkReply *)),&loop, SLOT(replyFinished(QNetworkReply *)));
         loop.exec();
      

      But if I replace 'this' param from connect by '&loop" I cant access anymore to 'replyFinished'.

      QObject::connect: No such slot QEventLoop::replyFinished(QNetworkReply *)
      

      How can I use Eventloop and access to class slot? Or, if u know how to solve problem in other way, I will be grateful.

      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #4

      @Andre-Muller You can't use QEventLoop to access class slot. Or you can, but not in the way you thought.
      The ususal way to use QEventLoop is:

      QEventLoop loop;
      QNetworkReply* rely = manager->post(request, params.query().toUtf8());
      connect(rely , SIGNAL(finished()), &loop, SLOT(quit()));
      loop.exec();
      //read from reply, or you can call the class slot / any class function here
      reply->deleteLater();
      

      or in the new syntax:

      connect(rely , &QNetworkReply::finished, &loop, &QEventLoop::quit);
      

      And are you creating a new QNetworkAccessManager for every request?
      Wow, that is not we usually do, either.

      A 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Andre-Muller said in Wait till POST request finished:

        database.requestor(req);

        Since the whole is async you have to wait until the reply is finished (what you're already doing and already executing dbase::replyFinished(). From there you can send a signal to process your data further.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        A 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @Andre-Muller said in Wait till POST request finished:

          database.requestor(req);

          Since the whole is async you have to wait until the reply is finished (what you're already doing and already executing dbase::replyFinished(). From there you can send a signal to process your data further.

          A Offline
          A Offline
          Andre.Muller
          wrote on last edited by
          #3

          @Christian-Ehrlicher send signal? but how prevent code running while it finishes? can You provide some examples? just didn't get how to make it working.

          and, btw, I thought, good option will be eventloop. I mean, it will pause running while data is getting.

          Here's sample:

          QEventLoop loop;
             manager->post(request, params.query().toUtf8());
             connect(manager, SIGNAL(finished(QNetworkReply *)),&loop, SLOT(replyFinished(QNetworkReply *)));
             loop.exec();
          

          But if I replace 'this' param from connect by '&loop" I cant access anymore to 'replyFinished'.

          QObject::connect: No such slot QEventLoop::replyFinished(QNetworkReply *)
          

          How can I use Eventloop and access to class slot? Or, if u know how to solve problem in other way, I will be grateful.

          B Christian EhrlicherC 2 Replies Last reply
          0
          • A Andre.Muller

            @Christian-Ehrlicher send signal? but how prevent code running while it finishes? can You provide some examples? just didn't get how to make it working.

            and, btw, I thought, good option will be eventloop. I mean, it will pause running while data is getting.

            Here's sample:

            QEventLoop loop;
               manager->post(request, params.query().toUtf8());
               connect(manager, SIGNAL(finished(QNetworkReply *)),&loop, SLOT(replyFinished(QNetworkReply *)));
               loop.exec();
            

            But if I replace 'this' param from connect by '&loop" I cant access anymore to 'replyFinished'.

            QObject::connect: No such slot QEventLoop::replyFinished(QNetworkReply *)
            

            How can I use Eventloop and access to class slot? Or, if u know how to solve problem in other way, I will be grateful.

            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #4

            @Andre-Muller You can't use QEventLoop to access class slot. Or you can, but not in the way you thought.
            The ususal way to use QEventLoop is:

            QEventLoop loop;
            QNetworkReply* rely = manager->post(request, params.query().toUtf8());
            connect(rely , SIGNAL(finished()), &loop, SLOT(quit()));
            loop.exec();
            //read from reply, or you can call the class slot / any class function here
            reply->deleteLater();
            

            or in the new syntax:

            connect(rely , &QNetworkReply::finished, &loop, &QEventLoop::quit);
            

            And are you creating a new QNetworkAccessManager for every request?
            Wow, that is not we usually do, either.

            A 1 Reply Last reply
            1
            • B Bonnie

              @Andre-Muller You can't use QEventLoop to access class slot. Or you can, but not in the way you thought.
              The ususal way to use QEventLoop is:

              QEventLoop loop;
              QNetworkReply* rely = manager->post(request, params.query().toUtf8());
              connect(rely , SIGNAL(finished()), &loop, SLOT(quit()));
              loop.exec();
              //read from reply, or you can call the class slot / any class function here
              reply->deleteLater();
              

              or in the new syntax:

              connect(rely , &QNetworkReply::finished, &loop, &QEventLoop::quit);
              

              And are you creating a new QNetworkAccessManager for every request?
              Wow, that is not we usually do, either.

              A Offline
              A Offline
              Andre.Muller
              wrote on last edited by
              #5

              @Bonnie so how to do right then? how to solve this problem? just no idea, fighting 2 days with it.

              B 1 Reply Last reply
              0
              • A Andre.Muller

                @Bonnie so how to do right then? how to solve this problem? just no idea, fighting 2 days with it.

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #6

                @Andre-Muller I've already written how to wait till finished for you, what do you not understand?
                The usual way for using QNetworkAccessManager is only create it once and keep reusing the instance (while in the same thread).

                A 1 Reply Last reply
                0
                • A Andre.Muller

                  @Christian-Ehrlicher send signal? but how prevent code running while it finishes? can You provide some examples? just didn't get how to make it working.

                  and, btw, I thought, good option will be eventloop. I mean, it will pause running while data is getting.

                  Here's sample:

                  QEventLoop loop;
                     manager->post(request, params.query().toUtf8());
                     connect(manager, SIGNAL(finished(QNetworkReply *)),&loop, SLOT(replyFinished(QNetworkReply *)));
                     loop.exec();
                  

                  But if I replace 'this' param from connect by '&loop" I cant access anymore to 'replyFinished'.

                  QObject::connect: No such slot QEventLoop::replyFinished(QNetworkReply *)
                  

                  How can I use Eventloop and access to class slot? Or, if u know how to solve problem in other way, I will be grateful.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Andre-Muller said in Wait till POST request finished:

                  but how prevent code running while it finishes?

                  Why do you want this? It will block your ui for no good reason.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • B Bonnie

                    @Andre-Muller I've already written how to wait till finished for you, what do you not understand?
                    The usual way for using QNetworkAccessManager is only create it once and keep reusing the instance (while in the same thread).

                    A Offline
                    A Offline
                    Andre.Muller
                    wrote on last edited by
                    #8

                    @Bonnie oh, sorry, didn't tested ur code, thought it's just syntax explanation.
                    All working well now. Thx a lot.

                    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