Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QDeclarativeImageProvider Class Reference

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

 #include <QDeclarativeImageProvider>

This class was introduced in Qt 4.7.

Public Types

enum ImageType { Image, Pixmap }

Public Functions

QDeclarativeImageProvider ( ImageType type )
virtual ~QDeclarativeImageProvider ()
ImageType imageType () const
virtual QImage requestImage ( const QString & id, QSize * size, const QSize & requestedSize )
virtual QPixmap requestPixmap ( const QString & id, QSize * size, const QSize & requestedSize )

Detailed Description

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

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

  • Loaded using QPixmaps rather than actual image files
  • Loaded asynchronously in a separate thread, if imageType() is ImageType::Image

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().

Note that the identifiers are case-insensitive, but the rest of the URL will be passed on with preserved case. For example, the below snippet would still specify that the image is loaded by the image provider named "myimageprovider", but it would request a different image than the above snippet ("Image.png" instead of "image.png").

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

If you want the rest of the URL to be case insensitive, you will have to take care of that yourself inside your image provider.

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 requestImage() or requestPixmap() method (depending on its 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 QPixmap images that are filled with the requested color:

 class ColorImageProvider : public QDeclarativeImageProvider
 {
 public:
     ColorImageProvider()
         : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
     {
     }

     QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
     {
         int width = 100;
         int height = 50;

         if (size)
             *size = QSize(width, height);
         QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
                        requestedSize.height() > 0 ? requestedSize.height() : 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:

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 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 QPixmap rather than 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.

See also QDeclarativeEngine::addImageProvider().

Member Type Documentation

enum QDeclarativeImageProvider::ImageType

Defines the type of image supported by this image provider.

ConstantValueDescription
QDeclarativeImageProvider::Image0The Image Provider provides QImage images. The requestImage() method will be called for all image requests.
QDeclarativeImageProvider::Pixmap1The Image Provider provides QPixmap images. The requestPixmap() method will be called for all image requests.

Member Function Documentation

QDeclarativeImageProvider::QDeclarativeImageProvider ( ImageType type )

Creates an image provider that will provide images of the given type.

QDeclarativeImageProvider::~QDeclarativeImageProvider () [virtual]

Destroys the QDeclarativeImageProvider

Note: The destructor of your derived class need to be thread safe.

ImageType QDeclarativeImageProvider::imageType () const

Returns the image type supported by this provider.

QImage QDeclarativeImageProvider::requestImage ( const QString & id, QSize * size, const QSize & requestedSize ) [virtual]

Implement this method to return the image with id. The default implementation returns an empty image.

The id is the requested image source, with the "image:" scheme and provider identifier removed. For example, if the image source was "image://myprovider/icons/home", the given id would be "icons/home".

The requestedSize corresponds to the Image::sourceSize requested by an Image element. If requestedSize is a valid size, the image returned should be of that size.

In all cases, size must be set to the original size of the image. This is used to set the width and height of the relevant Image if these values have not been set explicitly.

Note: this method may be called by multiple threads, so ensure the implementation of this method is reentrant.

QPixmap QDeclarativeImageProvider::requestPixmap ( const QString & id, QSize * size, const QSize & requestedSize ) [virtual]

Implement this method to return the pixmap with id. The default implementation returns an empty pixmap.

The id is the requested image source, with the "image:" scheme and provider identifier removed. For example, if the image source was "image://myprovider/icons/home", the given id would be "icons/home".

The requestedSize corresponds to the Image::sourceSize requested by an Image element. If requestedSize is a valid size, the image returned should be of that size.

In all cases, size must be set to the original size of the image. This is used to set the width and height of the relevant Image if these values have not been set explicitly.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web