Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. French
  4. "error: ‘sender’ was not declared in this scope" avec QNetworkReply
Forum Update on Monday, May 27th 2025

"error: ‘sender’ was not declared in this scope" avec QNetworkReply

Scheduled Pinned Locked Moved Solved French
12 Posts 4 Posters 1.1k Views
  • 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.
  • M Offline
    M Offline
    mpergand
    wrote on last edited by
    #2

    Salut,

    Il faut que la classe ConnectionToServer dérive de QObject
    et que la macro Q_OBJECT soit bien présente en tête de la classe (pour la définition des slots)

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Futuray Programming
      wrote on last edited by
      #3

      Non, cela sort une nouvelle erreur :
      error: Class contains Q_OBJECT macro but does not inherit from QObject

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #4

        C'est exactement ce que j'ai dit dans mon message précédent !

        1 Reply Last reply
        0
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #5

          Salut,

          La classe doit hériter de QObject et posséder la macro Q_OBJECT. Voir ici pour un exemple.

          Le code ci-dessus devrait se présenter de la manière suivante :

          
          class ConnectionToServer : public QObject {
              O_OBJECT
          
              public :
                  void connection (QPushButton *Button, QString Name, QString Passcode) {
                      Button->setEnabled(false);
                      QFile UrlFile(":/texts/connection-url.txt");
                      if(!UrlFile.open(QIODevice::ReadOnly)) { }
                      QTextStream UrlTextStream(&UrlFile);
                      QString UrlString;
                      UrlFile.close();
                      const QUrl MainServerUrl = QUrl(UrlString);
                      QNetworkRequest MainServerQuery(MainServerUrl);
                      MainServerQuery.setPriority(QNetworkRequest::HighPriority);
          
                      QNetworkAccessManager *m = new QNetworkAccessManager();
                      QNetworkReply *r = m->get(MainServerQuery);
                      QAbstractButton::connect(r, SIGNAL(finished()), nullptr, SLOT(LoginFinished(Button)));
                  }
          
              private:
                  void LoginFinished(QPushButton *Button) {
                      QNetworkReply *r = qobject_cast<QNetworkReply*>(sender());
                  }
              };
          }
          
          

          Cordialement,
          Axel

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          1
          • F Offline
            F Offline
            Futuray Programming
            wrote on last edited by
            #6

            Ah, d'accord, merci !

            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #7

              Je n'ai pas regardé le code en détail. J'espère qu'il est clair qu'il présente des problèmes plus ou moins importants :

              • Name et Passcode devraient être passés comme const & pour éviter les copies inutiles.
              • UrlString n'est jamais assigné, donc l'url ne sera jamais ouvert correctement.
              • QNetworkAccessManager *m est alloué mais pas supprimé => Memory leak !

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Futuray Programming
                wrote on last edited by
                #8

                Pour le dernier point, comment on règle le problème ?

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

                  Il faut l'effacer une fois qu'il n'est plus utilisé.

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

                  F 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Il faut l'effacer une fois qu'il n'est plus utilisé.

                    F Offline
                    F Offline
                    Futuray Programming
                    wrote on last edited by
                    #10

                    @SGaist Oui, certes, mais comment ?

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

                      En utilisant delete.

                      Ceci dit, l'architecture de ConnectionToServer n'est pas la meilleure. Y a-t-il d'autres objets dans l'application qui utilise QNetworkAccessManager ?

                      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
                      • Axel SpoerlA Offline
                        Axel SpoerlA Offline
                        Axel Spoerl
                        Moderators
                        wrote on last edited by
                        #12

                        @Futuray-Programming said in "error: ‘sender’ was not declared in this scope" avec QNetworkReply:

                        Oui, certes, mais comment ?

                        Il y a beaucoup de documentation autour de QNetworkAccessManager.
                        Vous pouvez y jeter un coup d'œil et travailler avec les extraits de code.

                        Software Engineer
                        The Qt Company, Oslo

                        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