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. Qt Network: urls not loading
QtWS25 Last Chance

Qt Network: urls not loading

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.5k 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.
  • S Offline
    S Offline
    spsingh
    wrote on last edited by
    #1

    I have code as follows:

    @void WebPageLoader::run()
    {
    QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);

    connect(networkManager, SIGNAL(finished(QNetworkReply *)),
    this, SLOT(PageLoaded(QNetworkReply *)));

    foreach(QUrl url, mUrls) {
    QNetworkRequest request(url);

    networkManager->get(request);
    }
    }@

    The slot PageLoaded is never called. Is there something else one needs to do to get it working?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mario
      wrote on last edited by
      #2

      Maybe you should consider changing the code a bit because you will keep a QNetworkAccessManager instance for each call to run until you delete the parent. Maybe you should just create a network manager in the parent's constructor and use that instance from the run method

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        The code should work, there's something different going wrong. Can you provide us a complete sample program demonstrating the behavior, please. Just a small main.cpp and a reduced class.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I think the culprit is line 9. You create the request on the stack instead of on the heap. That means the request gets deleted as soon as the code hits line 12.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mario
            wrote on last edited by
            #5

            Andre: That should not be the issue, that's how you use the request class.

            Example from the QNetworkAccessManager doc:

            @manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));@

            [EDIT: code formatting, Volker]

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

              Yes, you are right. The QNAM API looks a bit asymetric that way. Sorry for the noise!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                spsingh
                wrote on last edited by
                #7

                My Bad. WebPageLoader is a QThread which exits before the slot PageLoaded is called. What is the best practice to wait for all slots to have done their job before exiting the run?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  As QNAM is asynchronous already, there is hardly any need to put it into a separate thread.

                  If you really need to use a thread, call exec() in your run method. This starts a local event loop and keeps the thread running until you call quit().

                  Be aware of the implications regarding object owners, signals and slots. Peppe has written a great wiki article on "Threads, Events and QObjects":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects, don't miss to read it!

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mario
                    wrote on last edited by
                    #9

                    When do you delete the WebPageLoader? If that happens before you receive the response the NAM will be deleted and the request aborted.

                    My second guess is that you need to execute the event-loop if you're creating the NAM inside the thread, else no requests will be dispatched.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mario
                      wrote on last edited by
                      #10

                      Yeah, try it out w/o threading as Volker suggested since NAM is async. If you still have issues when processing the response maybe you can handle that in a separate thread

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        spsingh
                        wrote on last edited by
                        #11

                        thanks Volker for the wonderful link. The problem was that the emitter and the receiver are in separate threads causing a queued connection which requires an evwnt loop in the thread. caling exec in the thread solved the problem.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Good to hear, it runs now.

                          Did you consider dropping the QThread stuff? QNAM is asynchronous already, so it should be easy to put it into the main thread.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mario
                            wrote on last edited by
                            #13

                            But the processing of the reply (my slot) is running in the main thread, or I'm I wrong?

                            To prevent the main thread from blocking while doing heavy processing in the reply-slot, "QtConcurrent::run()":http://doc.qt.nokia.com/4.7/qtconcurrentrun.html can be used. It will execute a function in a separate thread. This should of course be used together with QFutureWatcher.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              I don't know - if you did not put it into a separate thread, it runs in the main loop, yes. If the main thread is blocking depends on how much work is done in your slot. If it's heavy-loaded, a separate thread can be a solution, but it has the drawback, that you cannot manipulate the GUI from it. You can send queued signals to slots of the GUI, though.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              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