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



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


  • Moderators

    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.



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


  • Moderators

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



  • 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


  • Moderators

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


  • Moderators

    @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



  • @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 ?


  • Moderators

    @cpper See comment from @raven-worx


  • Moderators

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



  • @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.


  • Moderators

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



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


  • Moderators

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



  • @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 ?


  • Moderators

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



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


  • Moderators

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



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


  • Moderators

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


  • Moderators

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



  • @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.


  • Moderators

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.