[Solved] Wanted: QQuickImageProvider examples

  • Hey Guys,

    I want to use QPixmaps in QML-Gui. For example:

    After some research I guess I should use QQuickImageProvider, but in the documentations the examples are missing: "QQuickImageProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html
    Does somebody know a good example to use it?

    Thank you very much!

  • Basically you implement one of
    @virtual QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize)
    virtual QPixmap requestPixmap(const QString & id, QSize * size, const QSize & requestedSize)
    virtual QQuickTextureFactory * requestTexture(const QString & id, QSize * size, const QSize & requestedSize)
    Create your provider setting type to one you implemented and add to qmlengine:
    @QQmlEngine::addImageProvider(QLatin1String("idForYourProvider"), new YourProvider(type));@

    Then you can use it from Qml using "image" schema with your id:

    @Image { source: "image://idForYourProvider/imageFromYourProvider.png" }@

    Seems like 4.8 documentation has the example:

  • Thank you for your help. I wrote something like this (doesnt make sense, only wanted to test it):

    @#include <QColor>

    class ImageProvider : public QQuickImageProvider
    : QQuickImageProvider(QQuickImageProvider::Pixmap)

    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
        int width = 160;
        int height = 100;
            *size = QSize(width,height);
        QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
                       requestedSize.height() > 0 ? requestedSize.height() : height);
        return pixmap;


    engine = new QQmlEngine;
    engine->addImageProvider(QLatin1String("provider"), new ImageProvider);
    viewer = new QQuickView;
    interface = viewer->rootObject();

    The property "lastpicture7" links to a source of an image. But I get following error: QML Image: Invalid image provider: image://provider/blue.png. Shoudnt it work this way? =( I used the 4.8 Example for QDeclarativeImageProvider

  • You are not using engine you created at fist line. And also documentation says that you have to add image provider before loading qml so your code in Gui.cpp should be:

    @ viewer = new QQuickView;
    viewer->engine()->addImageProvider(QLatin1String("provider"), new ImageProvider);
    interface = viewer->rootObject();

  • Thank you very much! The provider works perfect now. But i still have a little problem:

    The pixmaps provided by the ImageProvider have to change in runtime. So I call setProperty more than once. But when I change the property for the second time it doesnt call "requestPixmap". In the documentation it says:

    Image caching
    Images returned by a QDeclarativeImageProvider are automatically cached, similar to any image loaded by the QML engine. When an image with a "image://" prefix is loaded from cache, requestImage() and requestPixmap() will not be called for the relevant image provider. If an image should always be fetched from the image provider, and should not be cached at all, set the cache property to false for the relevant Image, BorderImage or AnimatedImage object.

    But I've got the same problems when I use @cache: false@
    for the relevant Image. Does anyone have an idea?

  • Okay, I solved it by using random numbers in my id's. Seems like he is only updating via requestPixmap when the id has changed.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.