QDeclarativeImageProvider

Inheritance diagram of QDeclarativeImageProvider

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtDeclarative.QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML.

PySide.QtDeclarative.QDeclarativeImageProvider is used to provide advanced image loading features in QML applications. It allows images in QML to be:

To specify that an image should be loaded by an image provider, use the “image:” scheme for the URL source of the image, followed by the identifiers of the image provider and the requested image. For example:

Image { source: "image://myimageprovider/image.png" }

This specifies that the image should be loaded by the image provider named “myimageprovider”, and the image to be loaded is named “image.png”. The QML engine invokes the appropriate image provider according to the providers that have been registered through QDeclarativeEngine.addImageProvider() .

An example

Here are two images. Their source values indicate they should be loaded by an image provider named “colors”, and the images to be loaded are “yellow” and “red”, respectively:

Column {
    Image { source: "image://colors/yellow" }
    Image { source: "image://colors/red" }
}

When these images are loaded by QML, it looks for a matching image provider and calls its PySide.QtDeclarative.QDeclarativeImageProvider.requestImage() or PySide.QtDeclarative.QDeclarativeImageProvider.requestPixmap() method (depending on its PySide.QtDeclarative.QDeclarativeImageProvider.imageType() ) to load the image. The method is called with the id parameter set to “yellow” for the first image, and “red” for the second.

Here is an image provider implementation that can load the images requested by the above QML. This implementation dynamically generates PySide.QtGui.QPixmap images that are filled with the requested color:

class ColorImageProvider (QDeclarativeImageProvider):
    def __init__(self):
        QDeclarativeImageProvider.__init__(self, QDeclarativeImageProvider.Pixmap)

    def requestPixmap(id, size, requestedSize):
        width = 100
        height = 50

        if size:
            size.setWidth(width)
            size.setHeight(height)

        if requestedSize.width() > 0:
            width = requestedSize.width()
        if requestedSize.height() > 0:
            height = requestedSize.height()

        pixmap = QPixmap(width, height)
        pixmap.fill(QColor(id).rgba())

        return pixmap

To make this provider accessible to QML, it is registered with the QML engine with a “colors” identifier:

int main(int argc, char *argv[])
{
    ...

    QDeclarativeEngine engine;
    engine->addImageProvider(QLatin1String("colors"), new ColorPixmapProvider);

    ...
}

Now the images can be successfully loaded in QML:

../../../../../src/qt4-x11-4.7.2/doc/src/images/imageprovider.png

A complete example is available in Qt’s examples/declarative/cppextensions/imageprovider directory. Note the example registers the provider via a plugin instead of registering it in the application main() function as shown above.

Asynchronous image loading

Image providers that support PySide.QtGui.QImage loading automatically include support for asychronous loading of images. To enable asynchronous loading for an Image source, set Image.asynchronous to true . When this is enabled, the image request to the provider is run in a low priority thread, allowing image loading to be executed in the background, and reducing the performance impact on the user interface.

Asynchronous loading is not supported for image providers that provide PySide.QtGui.QPixmap rather than PySide.QtGui.QImage values, as pixmaps can only be created in the main thread. In this case, if asynchronous is set to true , the value is ignored and the image is loaded synchronously.

class PySide.QtDeclarative.QDeclarativeImageProvider(type)
Parameters:typePySide.QtDeclarative.QDeclarativeImageProvider.ImageType
PySide.QtDeclarative.QDeclarativeImageProvider.ImageType
PySide.QtDeclarative.QDeclarativeImageProvider.imageType()
Return type:PySide.QtDeclarative.QDeclarativeImageProvider.ImageType
PySide.QtDeclarative.QDeclarativeImageProvider.requestImage(id, size, requestedSize)
Parameters:
Return type:

PySide.QtGui.QImage

PySide.QtDeclarative.QDeclarativeImageProvider.requestPixmap(id, size, requestedSize)
Parameters:
Return type:

PySide.QtGui.QPixmap