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. error: qnetworkaccessmanager
QtWS25 Last Chance

error: qnetworkaccessmanager

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.5k 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.
  • Juandav93J Offline
    Juandav93J Offline
    Juandav93
    wrote on last edited by
    #1

    trying to send a data by POST method to a php page receive these errors:

    QEventLoop : Can not be used without QApplication QObject :: connect : Can not connect ( null) :: aboutToQuit () to QNativeWifiEngine :: CloseHandle ()

    I work on windows, my code is:

    Variable::Variable(QObject *parent) :
       QObject(parent)
    {
    
    
    }
    
    
    void Variable::enviar()
    {
    man =  new QNetworkAccessManager(this);
       QNetworkRequest requerimiento(QUrl("http://127.0.0.1/ser/envio.php"));
       QByteArray postDatos;
    
       QUrlQuery consulta;
       consulta.addQueryItem("datos","hola");
       postDatos.append(consulta.toString());
       requerimiento.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
       requerimiento.setHeader(QNetworkRequest::ContentLengthHeader,postDatos.length());
    
       resp = man->post(requerimiento,postDatos);
    
       connect(man, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply *)));
    
       }
    

    Someone help me please

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @Juandav93 :)

      QEventLoop : Can not be used without QApplication

      Have you initialised a QApplication object somewhere (usually in your main function)? What does that initialisation look like? Based on the error message, I suspect the problem is outside of the code you've pasted.

      Tip: It's not likely to be the problem, but you should probably connect your signal handlers before issuing the network request. ie put connect before man->post.

      Cheers.

      1 Reply Last reply
      0
      • Juandav93J Offline
        Juandav93J Offline
        Juandav93
        wrote on last edited by Juandav93
        #3

        hi, thanks for answering , look:

        variable.h

        #ifndef VARIABLE_H
        #define VARIABLE_H
        
        #include <QObject>
        #include <QNetworkAccessManager>
        #include <QNetworkRequest>
        #include <QNetworkReply>
        #include <QUrlQuery>
        #include <QDateTime>
        #include <QFile>
        #include <QDebug>
        #include <QProcess>
        #include <QEventLoop>
        
        class Variable : public QObject
        {
         Q_OBJECT
        public:
            explicit Variable(QObject *parent = 0);
            void enviar();
        
        public slots:
            void replyFinished (QNetworkReply *reply);
        
        
        private:
           QNetworkAccessManager *man;
           QNetworkReply *resp;
           QProcess *proc;
        };
        
        #endif // VARIABLE_H
        
        

        main:

        #include "yei_threespace_api.h"
        #include <stdio.h>
        #include <signal.h>
        #include "variable.h"
        
        int main()
        {
        Variable d;
        d.enviar();
        return 0;
        }
        

        variable.cpp

        #include "variable.h"
        #include "yei_threespace_api.h"
        
        Variable::Variable(QObject *parent) :
            QObject(parent)
        {
        
        
        }
        
        
        void Variable::enviar()
        {
            QNetworkRequest requerimiento(QUrl("http://127.0.0.1/ser/envio.php"));
            QByteArray postDatos;
            QUrlQuery consulta;
            consulta.addQueryItem("datos","hola");
            postDatos.append(consulta.toString());
            requerimiento.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
            requerimiento.setHeader(QNetworkRequest::ContentLengthHeader,postDatos.length());
        
            man = new QNetworkAccessManager(this);
            connect(man, SIGNAL(finished()), this, SLOT(replyFinished(QNetworkReply *)));
            man->post(requerimiento,postDatos);
        
        
        
        }
        
        void Variable::replyFinished (QNetworkReply *reply)
        {
            QString data = reply->readAll();
        
        
           // reply->deleteLater();
        }
        

        I did what you recommended me but still continous the error.

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          @Juandav93 said:

          I did what you recommended me

          I can't see any QApplication being initialised in your main. It should look something like:

          int main(int argc, char **argv)
          {
              QApplication app(argc, argv);
              Variable d;
              d.enviar();
              return app.exec();
          }
          

          Given that you have no UI, you might want to use QCoreApplication instead, but if you're going to add a UI later, then stick with QApplication for now.

          Cheers.

          1 Reply Last reply
          1
          • Juandav93J Offline
            Juandav93J Offline
            Juandav93
            wrote on last edited by
            #5

            Hello, thank you very much for answering , the program sends the message to the PHP page but I now have a question, I have to do this every certain period of time, please me can help.

            Joel BodenmannJ 1 Reply Last reply
            0
            • Juandav93J Juandav93

              Hello, thank you very much for answering , the program sends the message to the PHP page but I now have a question, I have to do this every certain period of time, please me can help.

              Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by
              #6

              For any periodic task, use QTimer.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              1 Reply Last reply
              1
              • Juandav93J Offline
                Juandav93J Offline
                Juandav93
                wrote on last edited by
                #7

                @Joel-Bodenmann said:

                For any periodic task, use QTimer.

                Thank you very much you both for your answers : D

                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