Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slot connect/disconnect in network application
Qt 6.12 LTS is out! See what's new in the release blog

Slot connect/disconnect in network application

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 3.5k 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.
  • I Offline
    I Offline
    ingido
    wrote on last edited by ingido
    #1

    Hello!
    I have an network (basically chat) application in development.
    App have ApiHandler class which handles api requests. And other classes which interacts and update views after specific request has finished.
    I "dynamically" connect and disconnect slots for each specific request. But only first will called.
    Example of usage:

    void Friends::getFriends()
    {
        ApiHandler* a = new ApiHandler();
    
        connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
    
        a->makeRequest("getFriends", m); // m - parameters
    }
    

    getFriendsFinished

    void Friends::getFriendsFinished(QNetworkReply* reply)
    {
        // Process and update view...
    
        // Cleanup
        reply->deleteLater();
    
        ApiHandler* a = new ApiHandler();
    
        // Disconnects a slot
        disconnect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
    }
    

    But when i start another request from different classes, new connected slots never called. What is the best way to do this?

    ? 1 Reply Last reply
    0
    • I ingido

      Hello!
      I have an network (basically chat) application in development.
      App have ApiHandler class which handles api requests. And other classes which interacts and update views after specific request has finished.
      I "dynamically" connect and disconnect slots for each specific request. But only first will called.
      Example of usage:

      void Friends::getFriends()
      {
          ApiHandler* a = new ApiHandler();
      
          connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
      
          a->makeRequest("getFriends", m); // m - parameters
      }
      

      getFriendsFinished

      void Friends::getFriendsFinished(QNetworkReply* reply)
      {
          // Process and update view...
      
          // Cleanup
          reply->deleteLater();
      
          ApiHandler* a = new ApiHandler();
      
          // Disconnects a slot
          disconnect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
      }
      

      But when i start another request from different classes, new connected slots never called. What is the best way to do this?

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! In your connect and disconnect statements, you're not using the same ApiHandler instance but two different ones. You need to store a as a member variable of Friends and use only that one. Something like e.g.:

      class Friend :
      {
      // ...
      private:
          ApiHandler m_apiHandler;
      };
      
      // ...
      
      void Friends::getFriends()
      {
         connect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
      
      // ...
      
      
      void Friends::getFriendsFinished(QNetworkReply* reply)
      {
         disconnect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
      
      I 1 Reply Last reply
      1
      • ? A Former User

        Hi! In your connect and disconnect statements, you're not using the same ApiHandler instance but two different ones. You need to store a as a member variable of Friends and use only that one. Something like e.g.:

        class Friend :
        {
        // ...
        private:
            ApiHandler m_apiHandler;
        };
        
        // ...
        
        void Friends::getFriends()
        {
           connect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
        
        // ...
        
        
        void Friends::getFriendsFinished(QNetworkReply* reply)
        {
           disconnect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));
        
        I Offline
        I Offline
        ingido
        wrote on last edited by
        #3

        @Wieland thanks for reply! Already tried, but slots not called anyway. Only first connection for one time.
        Here the ApiHandler class

        class ApiHandler : public QObject
        {
            Q_OBJECT
        
        public:
            explicit ApiHandler(QObject *parent = 0);
            QNetworkReply* makeRequest(QString method, QMap<QString, QString> parameters);
            QNetworkAccessManager* getManager() {
                static QNetworkAccessManager* nam = new QNetworkAccessManager;
                return nam;
            }
        private:
            QUrl buildCall(QString method, QMap<QString, QString> parameters);
        
        signals:
        
        public slots:
            void replyFinished(QNetworkReply* reply);
            void slotReadyRead();
            void slotError(QNetworkReply::NetworkError error);
            void slotSslErrors(QList<QSslError> errorList);
        };
        

        Here the Friends class

        class Friends : public QObject
        {
            Q_OBJECT
        public:
            explicit Friends(QObject *parent = 0);
            void getFriends();
        
        signals:
        
        public slots:
            void getFriendsFinished(QNetworkReply* reply);
        
        };
        

        And here the usage

        Friends friends;
        
        friends.getFriends();
        
        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
        • Unsolved