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. Is there an easy way to download images from the web ?
QtWS25 Last Chance

Is there an easy way to download images from the web ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 13.2k 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.
  • jsulmJ jsulm

    @cpper Is it a https URL? Did you verify you can access the URL (for example in a browser)?
    You should verify that the slots were called, add

    qDebug() << Q_FUNC_INFO;
    

    to MainWindow::on_pushButton_clicked() and MainWindow::downloadFinished(QNetworkReply *reply)

    cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by cpper
    #8

    @jsulm said:

    qDebug() << Q_FUNC_INFO

    Yes, it's a valid https URL (https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg) . The slots were indeed called,:
    void __cdecl MainWindow::on_pushButton_clicked(void)
    void __cdecl MainWindow::downloadFinished(class QNetworkReply *)

    I thought the example would work with web urls too. What should I add to my code in order to work with web urls ?

    jsulmJ 1 Reply Last reply
    0
    • cpperC cpper

      @jsulm said:

      qDebug() << Q_FUNC_INFO

      Yes, it's a valid https URL (https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg) . The slots were indeed called,:
      void __cdecl MainWindow::on_pushButton_clicked(void)
      void __cdecl MainWindow::downloadFinished(class QNetworkReply *)

      I thought the example would work with web urls too. What should I add to my code in order to work with web urls ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @cpper See comment from @raven-worx

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #10

        Tested it with https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg, works for me.

        cpperC 1 Reply Last reply
        1
        • ? A Former User

          Tested it with https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg, works for me.

          cpperC Offline
          cpperC Offline
          cpper
          wrote on last edited by
          #11

          @Wieland
          It seems I have to install OpenSSL libraries.

          I found this article http://doc.qt.io/qt-5/opensslsupport.html but it don't understand what OpenSSL and Qt have to do with Android. I'll keep researching.

          raven-worxR 1 Reply Last reply
          0
          • cpperC cpper

            @Wieland
            It seems I have to install OpenSSL libraries.

            I found this article http://doc.qt.io/qt-5/opensslsupport.html but it don't understand what OpenSSL and Qt have to do with Android. I'll keep researching.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #12

            @cpper said:

            I found this article http://doc.qt.io/qt-5/opensslsupport.html but it don't understand what OpenSSL and Qt have to do with Android. I'll keep researching.

            as i said... You need it for decrypting the encrypted https connection.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • cpperC Offline
              cpperC Offline
              cpper
              wrote on last edited by cpper
              #13

              It seems the images I want to use in my program are from a http site. I used a random image from google to test the program, and picked one from a https site, not knowing what https actually is.

              The program works if I use http images (http://www.inmh.ro/sateliti/img/id813/id813_2016080205.png), so I think I'll skip the installing of OpenSSL for now.
              However, maybe one day I'll want to download from https sites, so could you please help with some instructions for installing OpenSSL ? I'm using Win64.

              raven-worxR 1 Reply Last reply
              0
              • cpperC cpper

                It seems the images I want to use in my program are from a http site. I used a random image from google to test the program, and picked one from a https site, not knowing what https actually is.

                The program works if I use http images (http://www.inmh.ro/sateliti/img/id813/id813_2016080205.png), so I think I'll skip the installing of OpenSSL for now.
                However, maybe one day I'll want to download from https sites, so could you please help with some instructions for installing OpenSSL ? I'm using Win64.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #14

                @cpper
                just put the 2 OpenSSL libraries (DLLs) beside your application exe.
                You just need to make sure that the compiler used for OpenSSL and your program matches (beside the debug/release and x86/x84 architecture). Otherwise the loading will fail.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                cpperC 1 Reply Last reply
                1
                • raven-worxR raven-worx

                  @cpper
                  just put the 2 OpenSSL libraries (DLLs) beside your application exe.
                  You just need to make sure that the compiler used for OpenSSL and your program matches (beside the debug/release and x86/x84 architecture). Otherwise the loading will fail.

                  cpperC Offline
                  cpperC Offline
                  cpper
                  wrote on last edited by cpper
                  #15

                  @raven-worx

                  I didn't try to install OpelSSL yet, but found out I could simply remove the 's' from "https". I don't know how clever this is, but it works. I don't get the "qt.network.ssl: QSslSocket: cannot call unresolved function X" outputs anymore, and can see the image.

                  @Wieland
                  You mentioned in the first post that this code leaks memory. Could you please explain how this happens ?

                  raven-worxR 1 Reply Last reply
                  0
                  • cpperC cpper

                    @raven-worx

                    I didn't try to install OpelSSL yet, but found out I could simply remove the 's' from "https". I don't know how clever this is, but it works. I don't get the "qt.network.ssl: QSslSocket: cannot call unresolved function X" outputs anymore, and can see the image.

                    @Wieland
                    You mentioned in the first post that this code leaks memory. Could you please explain how this happens ?

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #16

                    @cpper said:

                    I didn't try to install OpelSSL yet, but found out I could simply remove the 's' from "https". I don't know how clever this is, but it works. I don't get the "qt.network.ssl: QSslSocket: cannot call unresolved function X" outputs anymore and can see the image.

                    this works as long as the webserver "supports" it.
                    Some webserver may be configured to only allow https or even try to redirect you to the https url.

                    But redirect handling is anyway missing in your code i guess :)

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    cpperC 1 Reply Last reply
                    1
                    • raven-worxR raven-worx

                      @cpper said:

                      I didn't try to install OpelSSL yet, but found out I could simply remove the 's' from "https". I don't know how clever this is, but it works. I don't get the "qt.network.ssl: QSslSocket: cannot call unresolved function X" outputs anymore and can see the image.

                      this works as long as the webserver "supports" it.
                      Some webserver may be configured to only allow https or even try to redirect you to the https url.

                      But redirect handling is anyway missing in your code i guess :)

                      cpperC Offline
                      cpperC Offline
                      cpper
                      wrote on last edited by
                      #17

                      @raven-worx

                      My code is missing a lot. I'm a total beginner in Qt and just want to experiment with it.
                      I placed the two DLL where you said and now it works with https too :D

                      raven-worxR 1 Reply Last reply
                      0
                      • cpperC cpper

                        @raven-worx

                        My code is missing a lot. I'm a total beginner in Qt and just want to experiment with it.
                        I placed the two DLL where you said and now it works with https too :D

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #18

                        @cpper
                        good to hear.

                        In case the webserver redirects you to another url it wont work of course. In this case the reply only contains the information to the next url, but no data.
                        With this new url you have to start a new request which then hopefully contains your requested data.

                        See here for an example.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        1
                        • cpperC Offline
                          cpperC Offline
                          cpper
                          wrote on last edited by cpper
                          #19

                          Thanks raven, that goes to bookmarks.

                          I want to have the complete download process in one function, so after some research I found out I can use QEventLoop in order to wait for signal "finished". I wrote this function:

                          QPixmap download_from(const QString& url){
                          
                              QNetworkAccessManager nam;
                              QEventLoop loop;
                              QObject::connect(&nam,&QNetworkAccessManager::finished,&loop,&QEventLoop::quit);
                              QNetworkReply *reply = nam.get(QNetworkRequest(url));
                              loop.exec();
                          
                              QPixmap pm;
                              pm.loadFromData(reply->readAll());
                          
                              delete reply;
                              return pm;
                          }
                          

                          i'm calling it this way, from the "clicked" slot:

                          QPixmap pm=download_from("https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg");
                              ui->label->setPixmap(pm);
                          

                          The code works, and I can see the image, but I'd like to ask you if I'm doing something not recommended(redundant,slow,memory-expensive) , or if you think I could improve the function somehow.

                          jsulmJ raven-worxR 2 Replies Last reply
                          0
                          • cpperC cpper

                            Thanks raven, that goes to bookmarks.

                            I want to have the complete download process in one function, so after some research I found out I can use QEventLoop in order to wait for signal "finished". I wrote this function:

                            QPixmap download_from(const QString& url){
                            
                                QNetworkAccessManager nam;
                                QEventLoop loop;
                                QObject::connect(&nam,&QNetworkAccessManager::finished,&loop,&QEventLoop::quit);
                                QNetworkReply *reply = nam.get(QNetworkRequest(url));
                                loop.exec();
                            
                                QPixmap pm;
                                pm.loadFromData(reply->readAll());
                            
                                delete reply;
                                return pm;
                            }
                            

                            i'm calling it this way, from the "clicked" slot:

                            QPixmap pm=download_from("https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg");
                                ui->label->setPixmap(pm);
                            

                            The code works, and I can see the image, but I'd like to ask you if I'm doing something not recommended(redundant,slow,memory-expensive) , or if you think I could improve the function somehow.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #20

                            @cpper You're actually fighting against Qt. Qt is event driven, you should not try to "serialize" like you do now. What is the advantage? In the "clicked" slot you just start the download, as soon as finished signal arrives you're finished downloading and do what ever needs to be done.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            cpperC 1 Reply Last reply
                            1
                            • cpperC cpper

                              Thanks raven, that goes to bookmarks.

                              I want to have the complete download process in one function, so after some research I found out I can use QEventLoop in order to wait for signal "finished". I wrote this function:

                              QPixmap download_from(const QString& url){
                              
                                  QNetworkAccessManager nam;
                                  QEventLoop loop;
                                  QObject::connect(&nam,&QNetworkAccessManager::finished,&loop,&QEventLoop::quit);
                                  QNetworkReply *reply = nam.get(QNetworkRequest(url));
                                  loop.exec();
                              
                                  QPixmap pm;
                                  pm.loadFromData(reply->readAll());
                              
                                  delete reply;
                                  return pm;
                              }
                              

                              i'm calling it this way, from the "clicked" slot:

                              QPixmap pm=download_from("https://www.gimp.org/tutorials/Lite_Quickies/m51_hallas_big.jpg");
                                  ui->label->setPixmap(pm);
                              

                              The code works, and I can see the image, but I'd like to ask you if I'm doing something not recommended(redundant,slow,memory-expensive) , or if you think I could improve the function somehow.

                              raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by
                              #21

                              @cpper said:

                              The code works, and I can see the image, but I'd like to ask you if I'm doing something not recommended(redundant,slow,memory-expensive) , or if you think I could improve the function somehow.

                              beside the missing error and redirect handling its fine.
                              Alternatively you can move the whole code to a new custom "downloader" class and send the pixmap via a signal to the label.

                              @jsulm said:

                              @cpper You're actually fighting against Qt. Qt is event driven, you should not try to "serialize" like you do now.

                              i disagree. This is a valid usecase of an event loop. Even because Qt is event-driven, thats why there is an even loop ;)
                              I agree that i personally also prefer not using a local event loop, but still this solution is valid.

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @cpper You're actually fighting against Qt. Qt is event driven, you should not try to "serialize" like you do now. What is the advantage? In the "clicked" slot you just start the download, as soon as finished signal arrives you're finished downloading and do what ever needs to be done.

                                cpperC Offline
                                cpperC Offline
                                cpper
                                wrote on last edited by
                                #22

                                @jsulm

                                I want to have the download process in one function in order to be able to write things like :

                                QPixmap pm=download_from("url");
                                

                                In other words, I want to be able to return the downloaded image in another function.

                                ? 1 Reply Last reply
                                0
                                • cpperC cpper

                                  @jsulm

                                  I want to have the download process in one function in order to be able to write things like :

                                  QPixmap pm=download_from("url");
                                  

                                  In other words, I want to be able to return the downloaded image in another function.

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

                                  You still also need to consider errors, like: No network available, network resource not available, download takes two hours, download fails, downloaded bytes don't represent a valid image.

                                  1 Reply Last reply
                                  1
                                  • cpperC Offline
                                    cpperC Offline
                                    cpper
                                    wrote on last edited by cpper
                                    #24

                                    This is the only error handling I do:

                                    if(reply->error()!= QNetworkReply::NoError){
                                            return QPixmap();
                                        }
                                    

                                    I then check

                                    if(pm.isNull()){...}
                                    

                                    It works when I'm not connected to the internet or when the link isn't valid.

                                    I'm downloading the images from here: http://www.inmh.ro/sateliti/index.php?tip=2 . The link is based on the date and time of the pics, I managed to write a loop using QDateTime do download images from a specific time range. In the dropdown menu from the site you can see 8 images, but older images also still exist on the server.

                                    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