QQuickImageProviderclass provides an interface for supporting pixmaps and threaded image requests in QML. 更多 …
QQuickImageProvideris 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
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:
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
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”).
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.
Here are two images. Their
sourcevalues 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()orrequestPixmap()method (depending on itsimageType()) to load the image. The method is called with theidparameter 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
QPixmapimages that are filled with the requested color:class ColorImageProvider : public QQuickImageProvider { public: ColorImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) { } QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override { 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[]) { QQuickView view; QQmlEngine *engine = view.engine(); engine->addImageProvider(QLatin1String("colors"), new ColorImageProvider); view.setSource(QUrl::fromLocalFile(QStringLiteral("imageprovider-example.qml"))); view.show(); return app.exec(); }Now the images can be successfully loaded in QML:
![]()
见 Image Provider Example for the complete implementation. Note that the example registers the provider via a
plugininstead of registering it in the applicationmain()function as shown above.
Image providers that support
QImageor Texture loading automatically include support for asychronous loading of images. To enable asynchronous loading for an image source, set theasynchronous特性到truefor the relevant图像or BorderImage object. 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.To force asynchronous image loading, even for image sources that do not have the
asynchronousproperty set totrue, you may pass theQQmlImageProviderBase::ForceAsynchronousImageLoadingflag to the image provider constructor. This ensures that all image requests for the provider are handled in a separate thread.Asynchronous loading for image providers that provide
QPixmapis only supported in platforms that have the ThreadedPixmaps feature, in platforms where pixmaps can only be created in the main thread (i.e. ThreadedPixmaps is not supported) if asynchronous is set totrue, the value is ignored and the image is loaded synchronously.Asynchronous image loading for providers of type other than
ImageResponseare executed on a single thread per engine basis. That means that a slow image provider will block the loading of any other request. To avoid that we suggest usingQQuickAsyncImageProviderand implement threading on the provider side via aQThreadPoolor similar. See the Image Response Provider Example for a complete implementation.
Images returned by a
QQuickImageProviderare automatically cached, similar to any image loaded by the QML engine. When an image with a “image://” prefix is loaded from cache,requestImage()andrequestPixmap()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 thecache特性到falsefor the relevant图像or BorderImage 对象。另请参阅
addImageProvider()
QQuickImageProvider
(
type
[
,
flags=QQmlImageProviderBase.Flags()
]
)
¶
- param type
ImageType- param flags
标志
Creates an image provider that will provide images of the given
type
and behave according to the given
flags
.
PySide2.QtQuick.QQuickImageProvider.
requestImage
(
id
,
size
,
requestedSize
)
¶
id – unicode
size
–
QSize
requestedSize
–
QSize
QImage
Implement this method to return the image with
id
. The default implementation returns an empty image.
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”.
requestedSize
corresponds to the
Image::sourceSize
requested by an Image item. 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
图像
if these values have not been set explicitly.
注意
this method may be called by multiple threads, so ensure the implementation of this method is reentrant.
PySide2.QtQuick.QQuickImageProvider.
requestPixmap
(
id
,
size
,
requestedSize
)
¶
id – unicode
size
–
QSize
requestedSize
–
QSize
QPixmap
Implement this method to return the pixmap with
id
. The default implementation returns an empty pixmap.
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”.
requestedSize
corresponds to the
Image::sourceSize
requested by an Image item. 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
图像
if these values have not been set explicitly.
注意
this method may be called by multiple threads, so ensure the implementation of this method is reentrant.
PySide2.QtQuick.QQuickImageProvider.
requestTexture
(
id
,
size
,
requestedSize
)
¶
id – unicode
size
–
QSize
requestedSize
–
QSize
Implement this method to return the texture with
id
. The default implementation returns
None
.
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”.
requestedSize
corresponds to the
Image::sourceSize
requested by an Image item. 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
图像
if these values have not been set explicitly.
注意
this method may be called by multiple threads, so ensure the implementation of this method is reentrant.