Access network using QNetworkaccessmanger doesn't work on seperate class



  • Hi, i have created a c++ header file for network access and i am able access the network when i create instance in main.cpp, but it is not working(only network access like get,post... remains works fine) when i call it on a separate class.

    Sync_Network.h

    @#include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    #include <QDebug>
    #include <QUrl>
    class Sync_Network:public QNetworkAccessManager{
    Q_OBJECT
    public:
    Sync_Network(QString url,QObject parent=0):QNetworkAccessManager(parent),host(url){}
    virtual ~Sync_Network(){}
    bool Sync_Get(const QString path){
    get(QNetworkRequest(QUrl(host+""+path)));
    connect(this,SIGNAL(finished(QNetworkReply
    )),this,SLOT(showReply(QNetworkReply*)));
    return status;
    }
    bool Sync_Post(QString params,QString path=""){
    QByteArray data;
    data.append(params);
    post(QNetworkRequest(QUrl(host+""+path)),data);
    connect(this,SIGNAL(finished(QNetworkReply*)),this,SLOT(showReply(QNetworkReply*)));
    return status;
    }
    protected slots:
    virtual void showReply(QNetworkReply *reply){
    if(!reply->error()) qDebug()<<reply->readAll();
    else{
    status=false;
    qDebug()<<reply->errorString();
    }
    }
    private:
    bool status;
    QString host;
    };@

    tweet.cpp

    @#include "Sync_Network.h"

    Tweet::Tweet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Tweet)
    {
    ui->setupUi(this);
    Sync_Network s("http://www.capeconsultancy.com");
    s.Sync_Get("/aboutus");
    s.Sync_Post("<tweet><username>dinesh</username><passowrd>123456</password></tweet>");
    }@

    Please help me. Thanks in advance.



  • Hi,

    first of all, some general issues:

    • you should connect the signals in the constructor.
    • the way you do it, you connect them on each action, but after the action. It might happen, that the get is through, before your connect is done.

    From my point of view, I see no other errors...



  • Hi, i made the changes in the constructor but it won't works!

    @Sync_Network(QString url,QObject parent=0):QNetworkAccessManager(parent),host(url){
    connect(this,SIGNAL(finished(QNetworkReply
    )),this,SLOT(showReply(QNetworkReply*)));
    }@

    Thanks in advance.



  • You said it works, if you o it in main.cpp.
    can you show the working code?



  • main.cpp
    @#include <QtGui/QApplication>
    #include "Sync_Network.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Sync_Network s("http://www.capeconsultancy.com");
    s.Sync_Get("/aboutus");
    s.Sync_Post("<tweet><username>dinesh</username><passowrd>123456</password></tweet>");
    return a.exec();
    }
    @

    Thanks in advance



  • [quote author="dineshkumar" date="1296802238"]Hi, i have created a c++ header file for network access and i am able access the network when i create instance in main.cpp,

    but it is not working
    (only network access like get,post... remains works fine)
    when i call it on a separate class.

    [bold highlight by me, Volker][/quote]

    Now, does it work or does it not work? The bold highlighted phrases are contradictory. Please describe your problem, so that we are able to understand it.



  • Hello,

    I think there is some problem with post request:

    @
    bool Sync_Post(QString params,QString path=""){
    QByteArray data;
    data.append(params);
    post(QNetworkRequest(QUrl(host+""+path)),data);
    connect(this,SIGNAL(finished(QNetworkReply*)),this,SLOT(showReply(QNetworkReply*)));
    return status;
    }
    @
    @
    s.Sync_Post("<tweet><username>dinesh</username><passowrd>123456</password></tweet>");
    @

    You're calling post without specifying the path where you should post your request.


Log in to reply
 

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