Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [PROBLEM] How to store an image to reuse it
Forum Updated to NodeBB v4.3 + New Features

[PROBLEM] How to store an image to reuse it

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.1k 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.
  • M Offline
    M Offline
    Manatee
    wrote on last edited by
    #1

    Hi everybody. Im developing an app that needs to download an image from internet and show it into an Image item inserted into a stackview. When I do a push I pass the Image url and in the next view pushed the image appear after 100-300 milliseconds because the Image {} redownlod it again. This is bad for my app, I need to save the image downloaded the first time and reuse it in the view pushed it instead of redownload it again. How can it be accomplish this behaviour?
    Maybe the image is cached internally on my app somewhere?

    Thank you

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Download the image first and display the local copy.

      There are many ways to do this, for example you can use ImageProvider, or some custom caching class written in C++ which would add image(s) at known locations so that QML can load it.

      (Z(:^

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Manatee
        wrote on last edited by
        #3

        Hey, thanks for the reply. I look into imageProvider but it seems pretty "not fast" to be implemented. I check the example but not super sure how it works, Im trying right now with some images.
        There isn't something like the QSettings? That store data in predefined location for both Android and iOS and I dont even have to know the location?
        Thank you

        1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          Storing image into QSettings isn't a good idea. You can cache image in application's 'sandbox' and use QStandardPaths for crossplatform solution.

          JoeCFDJ M 2 Replies Last reply
          2
          • IntruderExcluderI IntruderExcluder

            Storing image into QSettings isn't a good idea. You can cache image in application's 'sandbox' and use QStandardPaths for crossplatform solution.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @IntruderExcluder said in [PROBLEM] How to store an image to reuse it:

            For example:
            QStandardPaths::PicturesLocation

            1 Reply Last reply
            0
            • IntruderExcluderI IntruderExcluder

              Storing image into QSettings isn't a good idea. You can cache image in application's 'sandbox' and use QStandardPaths for crossplatform solution.

              M Offline
              M Offline
              Manatee
              wrote on last edited by
              #6

              @IntruderExcluder Would you mind to give me an example or link on how to store image in the sandbox?
              The image will live only when app is running because everytime I open it up it will download the images. But it seems a bit tricky. I already saw QStandardPaths and it is perfect for the store location

              JoeCFDJ 1 Reply Last reply
              0
              • M Manatee

                @IntruderExcluder Would you mind to give me an example or link on how to store image in the sandbox?
                The image will live only when app is running because everytime I open it up it will download the images. But it seems a bit tricky. I already saw QStandardPaths and it is perfect for the store location

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @Manatee It is not tricky. QStandardPaths::PicturesLocation is a path. On Linux it is /home/username/Pictures. Save your image in this dir and load it whenever it is needed. Delete it when your app exits.

                M 1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Manatee
                  wrote on last edited by
                  #8

                  @JoeCFD Well so I can use QSaveFile in order to save it there?

                  1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @Manatee It is not tricky. QStandardPaths::PicturesLocation is a path. On Linux it is /home/username/Pictures. Save your image in this dir and load it whenever it is needed. Delete it when your app exits.

                    M Offline
                    M Offline
                    Manatee
                    wrote on last edited by
                    #9

                    @JoeCFD I found the solution now the problem is writing on Android, after downloading all the stuff and I try to write I get this error
                    "Problem opening save file "/storage/emulated/0/Pictures/tmpImg.jpg" for download "Operation not permitted"
                    Meanwhile If i create the file I get
                    "Problem opening save file "/storage/emulated/0/Pictures/tmpImg.jpg" for download "Permission Denied"
                    This is the code I used:

                    void DownloadManager::startDownload()
                    {
                        if (downloadQueue.isEmpty())
                        {
                            qDebug() << "[DownloadManager::startNextDownload]" << downloadedCount << " " << totalCount << " files download succesfully";
                            emit finished();
                            return;
                        }
                    
                        QUrl url = downloadQueue.dequeue();
                    
                    //    QString filename = saveFileName(url);
                        QString filename = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
                    
                        //Prendo nome primo elemento ed inserisco nella lista, poi lo rimuovo
                        if(!imagesName.isEmpty())
                        {
                            filename.append("/");
                            filename.append(imagesName.first());
                            imagesName.removeFirst();
                        }
                    
                        qDebug() << "[DownloadManager::startNextDownload] Filename " << filename;
                    
                        output.setFileName(filename);
                        if (!output.open(QIODevice::WriteOnly))
                        {
                            qDebug() << "[DownloadManager::startNextDownload] Problem opening save file " << filename << " for download " << output.errorString();
                            qDebug() << "[DownloadManager::startNextDownload] Skipping download";
                            startNextDownload();
                            return;                 // skip this download
                        }
                    
                        //Download the file into the location
                        QNetworkRequest request(url);
                        currentDownload = manager.get(request);
                        connect(currentDownload, &QNetworkReply::finished,
                                this, &DownloadManager::downloadFinished);
                        connect(currentDownload, &QNetworkReply::readyRead,
                                this, &DownloadManager::downloadReadyRead);
                    
                        // prepare the output
                        qDebug() << "[DownloadManager::startNextDownload] Downloading " << url;
                    }
                    
                    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