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 doesn't work
Qt 6.11 is out! See what's new in the release blog

QNetworkAccessManager doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 3.6k Views 2 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.
  • VolebabV Offline
    VolebabV Offline
    Volebab
    wrote on last edited by
    #1

    It just doesn't output the finished.

    QNetworkAccessManager manager;
    
    QNetworkRequest request;
    request.setUrl(QUrl("http://www.google.com"));
    
    auto response = manager.get(request);
    
    connect(response, &QNetworkReply::finished, [] () {
        qDebug() << "Finished";
    });
    
    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @Volebab,

      It looks like your QNetworkAccessManager is being destroyed as soon as you create the request (ie as it goes out of scope).

      Try:

      QNetworkAccessManager * manager = new QNetworkAccessManager(this);
      

      You need to ensure that the manager lives for as long as you want to be using the request you created with it.

      Cheers.

      VolebabV 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @Volebab,

        It looks like your QNetworkAccessManager is being destroyed as soon as you create the request (ie as it goes out of scope).

        Try:

        QNetworkAccessManager * manager = new QNetworkAccessManager(this);
        

        You need to ensure that the manager lives for as long as you want to be using the request you created with it.

        Cheers.

        VolebabV Offline
        VolebabV Offline
        Volebab
        wrote on last edited by
        #3

        @Paul-Colby You are right, thank you.
        How to ensure that this pointer isn't going to leak? I mean, I want to use it in a class, how to make sure that it's going to be deleted?

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          @Paul-Colby You are right, thank you.

          You're welcome :)

          How to ensure that this pointer isn't going to leak?

          There's lots of options, but you'll notice that in the QNetworkAccessManager constructor, I passed in this

          QNetworkAccessManager * manager = new QNetworkAccessManager(this);
          

          Assuming this is a QObject-derived class, then this sets up parent-child relationship such that when this gets destroyed, its destructor will also destroy manager.

          Have a read of Object Trees & Ownership - Construction/Destruction Order of QObjects.

          Cheers.

          VolebabV 1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            @Paul-Colby You are right, thank you.

            You're welcome :)

            How to ensure that this pointer isn't going to leak?

            There's lots of options, but you'll notice that in the QNetworkAccessManager constructor, I passed in this

            QNetworkAccessManager * manager = new QNetworkAccessManager(this);
            

            Assuming this is a QObject-derived class, then this sets up parent-child relationship such that when this gets destroyed, its destructor will also destroy manager.

            Have a read of Object Trees & Ownership - Construction/Destruction Order of QObjects.

            Cheers.

            VolebabV Offline
            VolebabV Offline
            Volebab
            wrote on last edited by Volebab
            #5

            @Paul-Colby I just noticed that if I try to use an actual slot it doesn't work anymore.

            connect(response, &QNetworkReply::finished, this, &MyClass::getResult);
            

            The getResult is never called.

            1 Reply Last reply
            0
            • Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #6

              @Volebab said:

              connect(response, &QNetworkReply::finished, this, &MyClass::getResult);

              Looks okay to me.

              Can you show us the updated code that sets up the request, as well as the implementation of the getResult function?

              VolebabV 1 Reply Last reply
              0
              • Paul ColbyP Paul Colby

                @Volebab said:

                connect(response, &QNetworkReply::finished, this, &MyClass::getResult);

                Looks okay to me.

                Can you show us the updated code that sets up the request, as well as the implementation of the getResult function?

                VolebabV Offline
                VolebabV Offline
                Volebab
                wrote on last edited by Volebab
                #7

                @Paul-Colby

                webservice.hpp:

                #ifndef WEBSERVICE_HPP
                #define WEBSERVICE_HPP
                
                #include <QObject>
                #include <QNetworkAccessManager>
                #include <QNetworkReply>
                
                class Webservice : public QObject
                {
                    Q_OBJECT
                public:
                    explicit Webservice(QObject *parent = 0);
                    virtual void ~Webservice();
                
                public slots:
                    void sendRequest();
                    
                private slots:
                    void getResult();
                    
                private:
                    QNetworkAccessManager *manager;
                    QNetworkReply *response;
                };
                
                #endif // WEBSERVICE_HPP
                

                webservice.cpp:

                #include "webservice.hpp"
                
                Webservice::Webservice(QObject *parent) : QObject(parent)
                {
                    manager = new QNetworkAccessManager(this);
                }
                
                void Webservice::~Webservice()
                {
                    manager->deleteLater();
                }
                
                void Webservice::sendRequest()
                {
                    QNetworkRequest request;
                    request.setUrl(QUrl("http://httpin.org/headers"));
                    
                    response = manager->get(request);
                
                    connect(response, &QNetworkReply::finished, this, &Webservice::getResult);
                }
                
                void Webservice::getResult()
                {
                    qDebug() << "finished";
                    qDebug() << response->readAll();
                    response->deleteLater();
                }
                
                1 Reply Last reply
                0
                • VolebabV Offline
                  VolebabV Offline
                  Volebab
                  wrote on last edited by Volebab
                  #8

                  I fixed it, first: The address was incorrect to it would never call finished but error signal, and second: it's not needed to create an instance of QNetworkAccessManager at all, it will work just fine with a stack allocation.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved