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. Asynchronous QQuickImageProvider
Forum Updated to NodeBB v4.3 + New Features

Asynchronous QQuickImageProvider

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.8k 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.
  • Mkowalik-MimesisM Offline
    Mkowalik-MimesisM Offline
    Mkowalik-Mimesis
    wrote on last edited by
    #1

    Hello,

    how can I implement asynchronous QQuickImageProvider? I want to load images from web-service and store them on disk. Unfortunately when I want to load images i must wait for all images, but i want to load them one by one. I found QQuickAsyncImageProvider class but there is no documentation. Could someone give me example of QQuickAsyncImageProvider?

    Here is my code:

    QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    {
        QStringList idSplit = id.split("/");
        QString thumbId = idSplit.last();
        QString thumbImageType = idSplit[idSplit.size()-1];
    
    //    if(!idSplit.contains("thumb", Qt::CaseInsensitive)) {
    //        qFatal("Wrong image id! Use this provider with \"thumb\"");
    //    }
        QString imagePath = IMAGES_PATH + "/" + thumbId + "_" + thumbImageType + ".png";
    
        QFile imageFile(imagePath);
        QImage p;
        if(imageFile.exists()) {
            p.load(imagePath);
            if(size) {
                *size = QSize(p.width(), p.height());
            }
        } else {
            QUrl imageUrl(QString("xxx"));
            QNetworkReply *reply = manager->get(QNetworkRequest(imageUrl));
            QEventLoop eventLoop;
            QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
            eventLoop.exec();
            if(reply->error() != QNetworkReply::NoError) {
                qDebug() << "Reply error: " << reply->errorString();
            } else {
                p = QImage::fromData(reply->readAll());
                p.save(imagePath);
            }
        }
    
        return p.scaled(requestedSize.width() > 0 ? requestedSize.width() : p.width(),
                        requestedSize.height() > 0 ? requestedSize.height() : p.height());
    }
    
    raven-worxR 1 Reply Last reply
    0
    • Mkowalik-MimesisM Mkowalik-Mimesis

      Hello,

      how can I implement asynchronous QQuickImageProvider? I want to load images from web-service and store them on disk. Unfortunately when I want to load images i must wait for all images, but i want to load them one by one. I found QQuickAsyncImageProvider class but there is no documentation. Could someone give me example of QQuickAsyncImageProvider?

      Here is my code:

      QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
      {
          QStringList idSplit = id.split("/");
          QString thumbId = idSplit.last();
          QString thumbImageType = idSplit[idSplit.size()-1];
      
      //    if(!idSplit.contains("thumb", Qt::CaseInsensitive)) {
      //        qFatal("Wrong image id! Use this provider with \"thumb\"");
      //    }
          QString imagePath = IMAGES_PATH + "/" + thumbId + "_" + thumbImageType + ".png";
      
          QFile imageFile(imagePath);
          QImage p;
          if(imageFile.exists()) {
              p.load(imagePath);
              if(size) {
                  *size = QSize(p.width(), p.height());
              }
          } else {
              QUrl imageUrl(QString("xxx"));
              QNetworkReply *reply = manager->get(QNetworkRequest(imageUrl));
              QEventLoop eventLoop;
              QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
              eventLoop.exec();
              if(reply->error() != QNetworkReply::NoError) {
                  qDebug() << "Reply error: " << reply->errorString();
              } else {
                  p = QImage::fromData(reply->readAll());
                  p.save(imagePath);
              }
          }
      
          return p.scaled(requestedSize.width() > 0 ? requestedSize.width() : p.width(),
                          requestedSize.height() > 0 ? requestedSize.height() : p.height());
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Mkowalik-Mimesis said:

      I found QQuickAsyncImageProvider class but there is no documentation.

      only if you don't search

      Basically you need to reimplement QQuickImageResponse. Trigger it's finished() signal whenever your download is finished and reimplement textureFactory() returning your QImage.

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

      Mkowalik-MimesisM 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Mkowalik-Mimesis said:

        I found QQuickAsyncImageProvider class but there is no documentation.

        only if you don't search

        Basically you need to reimplement QQuickImageResponse. Trigger it's finished() signal whenever your download is finished and reimplement textureFactory() returning your QImage.

        Mkowalik-MimesisM Offline
        Mkowalik-MimesisM Offline
        Mkowalik-Mimesis
        wrote on last edited by
        #3

        @raven-worx said:

        @Mkowalik-Mimesis said:

        I found QQuickAsyncImageProvider class but there is no documentation.

        only if you don't search

        Basically you need to reimplement QQuickImageResponse. Trigger it's finished() signal whenever your download is finished and reimplement textureFactory() returning your QImage.

        "No documentation" mean that there is no any example of use QQuickAsyncImageProvider. I have few questions about it:

        1. Where I should create QNetworkAccessManager for image download? In ProviderClass or ResponseClass?
        2. How can I set downloaded image to QQuickTextureFactory object?
        3. Which method from QQuickTextureFactory use to receive image?
        raven-worxR 1 Reply Last reply
        0
        • Mkowalik-MimesisM Mkowalik-Mimesis

          @raven-worx said:

          @Mkowalik-Mimesis said:

          I found QQuickAsyncImageProvider class but there is no documentation.

          only if you don't search

          Basically you need to reimplement QQuickImageResponse. Trigger it's finished() signal whenever your download is finished and reimplement textureFactory() returning your QImage.

          "No documentation" mean that there is no any example of use QQuickAsyncImageProvider. I have few questions about it:

          1. Where I should create QNetworkAccessManager for image download? In ProviderClass or ResponseClass?
          2. How can I set downloaded image to QQuickTextureFactory object?
          3. Which method from QQuickTextureFactory use to receive image?
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Mkowalik-Mimesis said:

          "No documentation" mean that there is no any example of use QQuickAsyncImageProvider.

          also not true ;)
          There is a reference to an full example in the doc page...

          @Mkowalik-Mimesis said:

          1. Where I should create QNetworkAccessManager for image download? In ProviderClass or ResponseClass?

          best would be to use a global QNAM, since it's not necessary to have multiple objects and pollute the memory.

          1. How can I set downloaded image to QQuickTextureFactory object?
          2. Which method from QQuickTextureFactory use to receive image?

          see the example how it should be done.

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

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved