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 send signals
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager doesn't send signals

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 570 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.
  • T Offline
    T Offline
    thecatishere
    wrote on last edited by
    #1

    When I call fromGoogle it never goes to the finished slot.
    Both connects return true, I've tried capturing QNetworkReply::readyRead and finished, they don't go to the slot either. I don't know if the signal is being send or not, I only know that it's not going in the connected slot.
    My question is why is the signal not being received in the slots?
    This is Qt 6.1.3

    networkmanager.cpp

    NetworkManager::NetworkManager(IAnswerer* answerer)
    {
        manager = new QNetworkAccessManager();
        connect(manager, &QNetworkAccessManager::finished,
                this, &NetworkManager::finished);
    
        connect(this, &NetworkManager::parsed,
                answerer, &IAnswerer::networkAnswer);
    }
    
    NetworkManager::~NetworkManager()
    {
        delete manager;
    }
    
    int NetworkManager::fromGoogle(QByteArray question)
    {
        manager->get(QNetworkRequest(QUrl(question)));
        return 0;
    }
    
    void NetworkManager::finished(QNetworkReply *reply)
    {
        qDebug() << "finished";
        emit parsed(reply->readAll().mid(50, 20));
    }
    
    

    networkmanager.h

    class NetworkManager : public QObject
    {
        Q_OBJECT
    private:
        QNetworkAccessManager *manager;
    public:
        explicit NetworkManager(IAnswerer * answerer);
        ~NetworkManager();
        int fromGoogle(QByteArray question);
    public slots:
        void finished(QNetworkReply *reply);
    signals:
        void parsed(QByteArray answer);
    };
    

    answerer.h

    class Answerer : IAnswerer
    {
        NetworkManager netManager;
    ...
    public:
        Answerer(QSettings &settings);
    ...
    public slots:
        void networkAnswer(QByteArray answer);
    

    Constructor in the source file:

    Answerer::Answerer(QSettings &_settings)
        : settings(_settings), netManager(this) { ... }
    

    ianswerer.h

    class IAnswerer : public QObject
    {
        Q_OBJECT
    public slots:
        virtual void networkAnswer(QByteArray answer) = 0;
    };
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should also connect the error related signals in case something goes wrong.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thecatishere
        wrote on last edited by thecatishere
        #3

        @SGaist , I forgot the mention that I already did that as well, and they didn't receive anything also.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @thecatishere said in QNetworkAccessManager doesn't send signals:

          Answerer

          How / where do you instantiate it?

          And creating a new QNetworkManager for every request is not good at all.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @thecatishere said in QNetworkAccessManager doesn't send signals:

            Answerer

            How / where do you instantiate it?

            And creating a new QNetworkManager for every request is not good at all.

            T Offline
            T Offline
            thecatishere
            wrote on last edited by thecatishere
            #5

            @Christian-Ehrlicher The manager is created in the constructor of NetworkManager, which is a single object instantiated in the constructor of Answerer. Answerer is again a single object that doesn't get deleted throughout the whole program runtime.
            reader.h

            class Reader
            {
                Answerer answerer;
            ...
            

            reader.cpp

            Reader::Reader()
                : answerer(settings) { ... }
            

            main.cpp

            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
                Reader reader;
            
                return a.exec();
            }
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Maybe you should try with a simple objects which calls QNetworkAccessManager to see if it works correct then. Then we also have a chance to reproduce your problem.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You do not show how you actually use it. From what you posted it looks like do not actually make any request.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                0
                • SGaistS SGaist

                  You do not show how you actually use it. From what you posted it looks like do not actually make any request.

                  T Offline
                  T Offline
                  thecatishere
                  wrote on last edited by thecatishere
                  #8

                  @SGaist I've created an example project and copy pasted the code from the networkmanager and it works there. I use it by calling netManager.fromGoogle(QByteArray(url)) in Answerer.

                  Edit:
                  I tried calling the NetworkManager in the main function before the initialization of Reader.
                  When I delete 'Reader reader;' it works, otherwise it doesn't. Reader class has nothing to do with the NetworkManager.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Well, as already written, with the bits you posted we can't determine what is going wrong as the code looks correct.

                    So if possible please provide a minimal compilable sample that shows your issue.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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