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. Access network using QNetworkaccessmanger doesn't work on seperate class
Forum Updated to NodeBB v4.3 + New Features

Access network using QNetworkaccessmanger doesn't work on seperate class

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.8k 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.
  • D Offline
    D Offline
    dineshkumar
    wrote on last edited by
    #1

    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.

    Dinesh

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

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

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dineshkumar
        wrote on last edited by
        #3

        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.

        Dinesh

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

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

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dineshkumar
            wrote on last edited by
            #5

            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

            Dinesh

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

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

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                situ117
                wrote on last edited by
                #7

                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.

                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