Asynchronous QQuickImageProvider
-
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()); }
-
@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.
-
@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:
- Where I should create QNetworkAccessManager for image download? In ProviderClass or ResponseClass?
- How can I set downloaded image to QQuickTextureFactory object?
- Which method from QQuickTextureFactory use to receive image?
-
@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:
- 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.
- How can I set downloaded image to QQuickTextureFactory object?
- Which method from QQuickTextureFactory use to receive image?
see the example how it should be done.