Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    4
    24
    10386
    Loading More Posts
    • 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.
    • cpper
      cpper last edited by cpper

      Is there a beginner-friendly method to download and image from a known url (and later display it in a QLabel) ?

      1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by A Former User

        Hi! You can use QNetworkAccessManager for this:

        void MainWindow::on_pushButton_clicked()
        {
            QNetworkAccessManager *nam = new QNetworkAccessManager(this);
            connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::downloadFinished);
            const QUrl url = QUrl("http://computer/a.jpg");
            QNetworkRequest request(url);
            nam->get(request);
        }
        
        void MainWindow::downloadFinished(QNetworkReply *reply)
        {
            QPixmap pm;
            pm.loadFromData(reply->readAll());
            ui->label->setPixmap(pm);
        }
        

        The code above works, but you also need to take care of network errors etc. Also this code leaks memory.

        1 Reply Last reply Reply Quote 3
        • cpper
          cpper last edited by

          I compiled your code, but I get 12 unresolved externals.

          1 Reply Last reply Reply Quote 0
          • ?
            A Former User last edited by

            QNetworkAccessManager is part of the Qt Network module, so you need to add QT += network to your *.pro file.

            1 Reply Last reply Reply Quote 3
            • cpper
              cpper last edited by

              Thanks, that was causing the problems.
              However, the code doesn't work yet, I click on my button but the image is not displayed. I also get this output:

              qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
              qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
              qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
              qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
              qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error

              jsulm raven-worx 2 Replies Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @cpper last edited by

                @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)

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

                cpper 1 Reply Last reply Reply Quote 1
                • raven-worx
                  raven-worx Moderators @cpper last edited by raven-worx

                  @cpper said:

                  However, the code doesn't work yet, I click on my button but the image is not displayed. I also get this output:

                  qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
                  qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
                  qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
                  qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
                  qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error

                  these errors are mostly raised when you are missing the OpenSSL libraries to access https URLs

                  --- 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 Reply Quote 2
                  • cpper
                    cpper @jsulm last edited by 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 ?

                    jsulm 1 Reply Last reply Reply Quote 0
                    • jsulm
                      jsulm Lifetime Qt Champion @cpper last edited by

                      @cpper See comment from @raven-worx

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

                      1 Reply Last reply Reply Quote 1
                      • ?
                        A Former User last edited by

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

                        cpper 1 Reply Last reply Reply Quote 1
                        • cpper
                          cpper @Guest last edited by

                          @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-worx 1 Reply Last reply Reply Quote 0
                          • raven-worx
                            raven-worx Moderators @cpper last edited by

                            @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 Reply Quote 1
                            • cpper
                              cpper last edited by 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-worx 1 Reply Last reply Reply Quote 0
                              • raven-worx
                                raven-worx Moderators @cpper last edited by

                                @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

                                cpper 1 Reply Last reply Reply Quote 1
                                • cpper
                                  cpper @raven-worx last edited by 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-worx 1 Reply Last reply Reply Quote 0
                                  • raven-worx
                                    raven-worx Moderators @cpper last edited by 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 :)

                                    --- 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

                                    cpper 1 Reply Last reply Reply Quote 1
                                    • cpper
                                      cpper @raven-worx last edited by

                                      @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-worx 1 Reply Last reply Reply Quote 0
                                      • raven-worx
                                        raven-worx Moderators @cpper last edited by

                                        @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 Reply Quote 1
                                        • cpper
                                          cpper last edited by 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.

                                          jsulm raven-worx 2 Replies Last reply Reply Quote 0
                                          • jsulm
                                            jsulm Lifetime Qt Champion @cpper last edited by

                                            @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

                                            cpper 1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post