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. QDeclarativeImageProvider with images loaded from network
Forum Updated to NodeBB v4.3 + New Features

QDeclarativeImageProvider with images loaded from network

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 5 Posters 5.7k 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
    minimoog77
    wrote on last edited by
    #1

    I have something like this:

    @MyImageProvider::MyImageProvider(QObject *parent) : QObject(parent),
    --QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
    {
    ...
    }

    QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    {
    //download from web
    ...
    QNetworkReply *reply = m_netManager->get(request);
    connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));

    //What to return? Image is not loaded yet
    

    }

    void MyImageProvider::downloadFinished()
    {
    //How to inform declarative engine that image is loaded?
    }@

    I want to download images from web, and do some operations on them. The problem is QtNetwork doesn't offer blocking I/O, so it's not simple to return image in requestImage method. I could do some blocking with entering event loop, but, in my opionion, is very hackish.

    Any other solution?

    Thanks in advance,
    Toni

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mario
      wrote on last edited by
      #2

      I would like to see a more "async" api for image providers.

      Do you think you could use a QWaitCondition in the requestImage-method and have another thread downloading the images and notify when it's done?

      Please update this thread if you create a solution.

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

        Well, QDeclarativeImageProvider is already supporting async loading (if you are using QImage and use Image::asynchronous to true): loading image will be in low priority thread and will not block the UI.

        I think QDeclarativeImageProvider was not designed (or was not planned not to be designed) to be used with loading images from network.

        Anyway, my solution will be, after I fill up bug/suggestion report, is to use my own image QML element subclassing QDeclarativeItem.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mario
          wrote on last edited by
          #4

          Yes, there's support for executing the requestImage method async but you still need to block in requestImage. You can't simple call requestImage and get a call back (signal) later when the image is loaded.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            huluyige
            wrote on last edited by
            #5

            did you guys got a solution? i run into the same problem.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mario
              wrote on last edited by
              #6

              huluyige: No, I haven't done anything on this yet because I'm not needing it for the moment :)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                minimoog77
                wrote on last edited by
                #7

                Neither me. :)

                But my idea was, to start download images from network, do some blocking ( using 'waiting in a local event loop' http://doc.qt.nokia.com/qq/qq27-responsive-guis.html ), after that return the downloaded image.

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

                  HI,
                  I just had a similar issue. I created new DeclaraticeItem ImageCache with property "source". When setting the "source" of ImageCache in C++, check if it exists, if it does emit signal sourceUpdated and if not query network with url, when network reply finished and new image retrieved save it and then emit sourceUpdated.

                  Here is the qml:
                  @ImageCache{
                  id: imagecache
                  source: image://myimageprovider/mypic.png
                  Image{
                  id: image
                  }
                  onSourceUpdated:{
                  image.source=source
                  }
                  }@

                  Here is the C++ :
                  @class ImageCache : public QDeclarativeItem
                  {
                  Q_OBJECT
                  Q_PROPERTY(QString source READ source WRITE setSource );

                  public:
                  QString source(){
                  return src;
                  }
                  void setSource(QString src){
                  this.src=src
                  if(exists(src)){
                  emit sourceUpdated;
                  else{
                  QNetworkReply *reply = m_netManager->get(request);
                  connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));

                  }
                  }
                  void downloadFinished()
                  {
                  QImage img;
                  img.loadFromData(reply->readAll());
                  img.save(<path>)
                  emit sourceUpdated;
                  }
                  signal:
                  void sourceUpdated();

                  private:
                  QString src;
                  }@

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tasuku
                    wrote on last edited by
                    #9

                    I created a class in C++ that loads a image and stores cache locally then returns local cache path through cache property.

                    QML code is like this.
                    @ // MyImage.qml
                    Item {
                    width: image.sourceSize.width
                    height: image.sourceSize.height
                    property alias source: imageLoader.source
                    property alias smooth: image.smooth

                    Image {
                        id: image
                        anchors.fill: parent
                        source: imageLoader.cache
                    }
                    
                    ImageLoader {
                        id: imageLoader
                    }
                    

                    }@

                    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