Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renew QQuickImageProvider request

Tags:

qt

qt5

qml

To show some Pixmap in QML from a C++ model, I used a QQuickImageProvider:

class ImageProvider : public QQuickImageProvider
{
public:
    ImageProvider(MyModel *model) : QQuickImageProvider(QQuickImageProvider::Pixmap), _model(model) { }

    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override
    {
        Q_UNUSED(requestedSize);
        int width = 160;
        int height = 120;

        QString name = id.left(id.indexOf("*"));
        if (size) *size = QSize(width, height);        
        return _model->findThumbnail(name); // retrieve image in the model
    }

private:
    MyModel *_model;
};

The model is ready on startup, but the images are populated at run-time, slowly. When the QML page is loaded it requests to the image provider the pixmaps but they are not ready yet.

After some time they are available in the model, but I don't know how to tell to the QML Image object to renew the request to the provider.

like image 889
Mark Avatar asked Aug 07 '26 21:08

Mark


2 Answers

QQuickImageProvider has a well documented asynchronous mode, which you can force by passing QQmlImageProviderBase::ForceAsynchronousImageLoading to the provider constructor.

The way I understand it is that requests will be handled by a dedicated thread and delivered when done. Which means that what you should do is stall the thread until the image data can be provided, ideally busy with fetching said data.

This also would cause images to be loaded in the order they are requested by the application rather than some other order, presumably that of the model items.

like image 105
dtech Avatar answered Aug 09 '26 10:08

dtech


The solution is simple, create a new role that indicates that the image is fully loaded.

 setData(index, false, IsLoadedRole);
 // finished loading
 setData(index, true, IsLoadedRole);

*.qml

Image { 
    source: isLoaded ? "image://MyImageProvider/" : ""}
}
like image 44
eyllanesc Avatar answered Aug 09 '26 11:08

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!