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 signal never triggers the slot
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager - finished signal never triggers the slot

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.8k 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.
  • A Offline
    A Offline
    Alaa Elrifaie
    wrote on last edited by
    #1

    I'm trying to use QNetworkAcessManager to get the source of a url .. But it seems that there's a problem with the signal-slot complex!

    my onFinished() is never triggered! Why?

    @void Worker::start(QString url)
    {
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
    QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
    this, SLOT(onError(QNetworkReply::NetworkError)));
    }

    void Worker::onFinished()
    {
    qDebug() << "Hell yeah! slot has been triggered finally!";
    }

    void Worker::onError(QNetworkReply::NetworkError error)
    {
    qDebug() << "Error!";
    }@

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

      Hi,

      You should also connect the error signal, you might be having something going wrong somewhere else.

      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
      • A Offline
        A Offline
        Alaa Elrifaie
        wrote on last edited by
        #3

        [quote author="SGaist" date="1406406722"]Hi,

        You should also connect the error signal, you might be having something going wrong somewhere else.[/quote]

        Actually, I've done that as shown in the first post ( edited ), but also nothing happens at all!

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

          What does the url look like ?

          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
          • A Offline
            A Offline
            Alaa Elrifaie
            wrote on last edited by
            #5

            [quote author="SGaist" date="1406410505"]What does the url look like ?[/quote]

            I've tried everything actually, like for example:
            http://www.google.com
            http://qt-project.org
            http://cplusplus.com
            http://microsoft.com

            So it's hopefully not a url-related problem. :)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Alaa Elrifaie
              wrote on last edited by
              #6

              Ok can you guys just give me a working piece of code? regardless of helping me with my specific problem, because I completely believe now that it's unsolvable! :(

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

                Your code is fine, except that you don't need to create a QNetworkAccessManager for each request. Just make it a member variable and use the same manager for all your request.

                You have code examples using QNetworkAccessManager directly in the documentation.

                Did you check that the network is up and running ?

                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
                • A Offline
                  A Offline
                  Alaa Elrifaie
                  wrote on last edited by
                  #8

                  [quote author="SGaist" date="1406494121"]Your code is fine, except that you don't need to create a QNetworkAccessManager for each request. Just make it a member variable and use the same manager for all your request.

                  You have code examples using QNetworkAccessManager directly in the documentation.

                  Did you check that the network is up and running ?[/quote]
                  I've also tried this:
                  @ QNetworkAccessManager manager;
                  QNetworkRequest request;
                  request.setUrl(QUrl(url));
                  request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
                  QNetworkReply *response = manager.get(request);
                  connect(response, SIGNAL(finished()), this, SLOT(onFinished()));@
                  But it doesn't work also, I checked every example & also searched all over the web, every asynchronous code won't work!

                  I've made it synchronously, but I do need it asynchronously!

                  The network connection is up & running, unless it'd fire up the error signal.

                  :(

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

                    That last version won't work since manager get's out of scope at the end of the function call.

                    Do you have a QCoreApplication event loop running ?

                    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