×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    QNetworkRequest And signal/slots

    My code is:
    Code:
    #ifndef HTTPACTION_H
    #define HTTPACTION_H
    
    #include <QtNetwork>
    #include <QNetworkAccessManager>
    #include <QUrl>
    
    class HttpAction : public QObject
    {
        Q_OBJECT
    
    public:
        HttpAction();
        void startRequest();
        void setUrl(QUrl url);
    
    
    private slots:
        void httpFinished();
    
    private:
        QUrl httpUrl;
        QNetworkAccessManager qnam;
        QNetworkReply *reply;
    };
    
    #endif
    
    ----------------------
    
    HttpAction::HttpAction()
    {
        
    }
    
    void HttpAction::startRequest()
    {
        qWarning("2");
        reply = qnam.get(QNetworkRequest(httpUrl));
    
        connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
    }
    
    void HttpAction::httpFinished()
    {
    
         qWarning("3");
    
         if (reply->error() == QNetworkReply::NoError) {
    
             QByteArray bytes = reply->readAll();
         }
    
        reply->deleteLater();
    }
    
    void HttpAction::setUrl(QUrl url) {
    
        httpUrl = url;
    }
    
    
    --------------------
    
    #include "httpaction.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushbutton_clicked()
    {
    
        qWarning("1");
    
        HttpAction* http = new HttpAction;
        http->setUrl(QUrl("http://google.com"));
        http->startRequest();
    
        qWarning("4");
    
    }
    
    --------------
    And output:
    1
    2
    4
    3

    Why httpFinished() is executed after qWarning("4") ? I want to make http requests class to use everywhere in my app.

  2. #2
    Regular Contributor
    Join Date
    Apr 2010
    Location
    Surat, India
    Posts
    277

    Re: QNetworkRequest And signal/slots

    Quote Originally Posted by VidoX View Post
    My code is:

    And output:
    1
    2
    4
    3

    Why httpFinished() is executed after qWarning("4") .
    because qnam.get(.......) is asynchronous method which returns immediately after sending request to server. It does not wait for the response. When you make a request then it's state is managed by QNetworkAccessManager. so in your code; qnam is responsible for handling resonse; and you should connect qnam as below
    Code:
     connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    I am not sure why you are using reply instead of qnam.

    As soon as qnam will get response from server. It will emit signal finished() and finishedSlot will be executed. Read more about signals, slots and asynchronous calls.
    Thanks,
    Rahul Kulshreshtha
    Please choose Notification type as "Instant Email Notification". It must be default.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Re: QNetworkRequest And signal/slots

    Thanks, but changed to nam and it's the same

    Code:
    #ifndef HTTPACTION_H
    #define HTTPACTION_H
    
    #include <QtNetwork>
    #include <QNetworkAccessManager>
    #include <QUrl>
    
    class HttpAction : public QObject
    {
        Q_OBJECT
    
    public:
        HttpAction();
        void startRequest();
        void setUrl(QUrl url);
    
    
    private slots:
        void httpFinished(QNetworkReply* reply);
    
    private:
        QUrl httpUrl;
    
    };
    
    #endif
    
    ----------------------
    
    HttpAction::HttpAction()
    {
        
    }
    
    void HttpAction::startRequest()
    {
        qWarning("2");
    
        QNetworkAccessManager* nam;
        nam = new QNetworkAccessManager(this);
    
        connect(nam, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(httpFinished(QNetworkReply*)));
    
        QNetworkReply* reply = nam->get(QNetworkRequest(httpUrl));
    
    }
    
    void HttpAction::httpFinished(QNetworkReply* reply)
    {
    
         qWarning("3");
    
         if (reply->error() == QNetworkReply::NoError) {
    
             QByteArray bytes = reply->readAll();
         }
    
        reply->deleteLater();
    }
    
    void HttpAction::setUrl(QUrl url) {
    
        httpUrl = url;
    }
    
    
    --------------------
    
    #include "httpaction.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushbutton_clicked()
    {
    
        qWarning("1");
    
        HttpAction* http = new HttpAction;
        http->setUrl(QUrl("http://google.com"));
        http->startRequest();
    
        qWarning("4");
    
    }
    
    --------------

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Re: QNetworkRequest And signal/slots

    Found a solution:

    Code:
    void HttpAction::startRequest()
    {
        qWarning("2");
    
        QNetworkAccessManager* nam;
        nam = new QNetworkAccessManager(this);
    
        connect(nam, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(httpFinished(QNetworkReply*)));
    
        QNetworkReply* reply = nam->get(QNetworkRequest(httpUrl));
    
      QEventLoop eventLoop;
    
        connect(nam, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
        eventLoop.exec();
    
    }

Similar Threads

  1. Replies: 3
    Last Post: 2010-10-25, 19:20
  2. QNetworkRequest Authentication
    By wenz in forum Qt
    Replies: 2
    Last Post: 2010-10-18, 13:19
  3. Replies: 3
    Last Post: 2010-08-02, 12:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×