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. QNetworkAccessManager::finished(QNetworkReply * reply) signal doesn't work right.
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager::finished(QNetworkReply * reply) signal doesn't work right.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 4.4k 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.
  • S Offline
    S Offline
    SetBetterPass
    wrote on last edited by
    #1

    Hi all, can someone help me with my little problem?

    I have this small function which I expected will check if I am connected to the Internet and if so it will send get request to Rurl
    and after downloading as hit finished(QNetworkReply*) signal it will go to finishedSlot(QNetworkReply*)

    Code:

    @void downloadrequest::sendRequest(QUrl Rurl){

    QNetworkConfigurationManager *mgr = new QNetworkConfigurationManager();
    if(!mgr->isOnline()){QMessageBox::warning(0, QString("Warning"), QString("you are not connected to the Internet"));}
    else {QNetworkAccessManager *nam = new QNetworkAccessManager(this);
    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(finishedSlot(QNetworkReply*)));
    QNetworkReply* reply = nam->get(QNetworkRequest(Rurl));}
    

    }@

    my problem is it never hit finished(QNetworkReply*) and I got stuck there and will never get to finishedSlot, I am pretty sure I am connected to the Internet and website I tested it with (www.google.com) is online and responding

    I am using Qt creator 2.4.1 with Qt 4.8.0.

    Thanks for help.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Perhaps you should also connect to the error signal of the returned reply.

      By the way: who is responsible for destructing the QNAM in your code? The idea is that you use a single QNAM in your application, instead of creating a new QNAM for every request.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SetBetterPass
        wrote on last edited by
        #3

        Forgot about destructing it thanks for reminding me about it :)
        and I have already connected error signal in finishedSlot. I have also put it into sendRequest function but no error was reported, I am confused about this because it's really basic function and i can't find out why it doesn't work...

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          Further to Andre's comments (with which I concur), the following example works just fine for me:

          widget.h
          @
          #ifndef WIDGET_H
          #define WIDGET_H

          #include <QtGui>
          #include <QtNetwork>
          #include <QDebug>

          class Widget : public QWidget{
          Q_OBJECT
          public:
          Widget(QWidget parent=0) : QWidget(parent)
          {
          mgr=new QNetworkConfigurationManager;
          nam=new QNetworkAccessManager(this);
          connect(nam, SIGNAL(finished(QNetworkReply
          )),
          this, SLOT(replyFinished(QNetworkReply*)));

              QVBoxLayout *l=new QVBoxLayout(this);
              QPushButton *b=new QPushButton("Send", this);
              connect(b, SIGNAL(clicked()), this, SLOT(send()));
              l->addWidget(b);
          }
          

          private:
          QNetworkConfigurationManager *mgr;
          QNetworkAccessManager *nam;

          public slots:
          void send(){
          if(!mgr->isOnline()){
          qDebug() << "Not connected to the internet...";
          return;
          }
          nam->get(QNetworkRequest(QUrl("http://www.google.co.uk/")));
          }

          void replyFinished(QNetworkReply* reply){
              qDebug() << "Reply:\n" << reply->readAll();
              reply->deleteLater();
          }
          

          };

          #endif // WIDGET_H
          @

          main.cpp
          @
          #include <QtGui>
          #include <widget.h>

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          Widget w;
          w.show();

          return a.exec&#40;&#41;;
          

          }
          @

          Hope this helps ;o)

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SetBetterPass
            wrote on last edited by
            #5

            I used a network monitor to see what happens on my ethernet port after using this function and I'm even more confused. It sent DNS request and got reply from DNS server, but as it sent http get request to google server (I also tried other servers) no reply came back and I have no idea why.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              radzaw
              wrote on last edited by
              #6

              .

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NicuPopescu
                wrote on last edited by
                #7

                you could connect even QNetworkReply* reply finished() or downloadProgress() to slots ... finished() sent by QNetworkAccessManager is in tandem(one after another ) with that sent by its QNetworkReply

                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