error: qnetworkaccessmanager



  • 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



  • 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.



  • 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.



  • @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.



  • 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.



  • For any periodic task, use QTimer.



  • @Joel-Bodenmann said:

    For any periodic task, use QTimer.

    Thank you very much you both for your answers : D


Log in to reply
 

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