1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qdeclarativeimageprovider.cpp -->
<title>Qt 4.8: QDeclarativeImageProvider Class Reference</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="modules.html">Modules</a></li>
<li><a href="qtdeclarative.html">QtDeclarative</a></li>
<li>QDeclarativeImageProvider</li>
</ul>
</div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-types">Public Types</a></li>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#an-example">An example</a></li>
<li class="level2"><a href="#asynchronous-image-loading">Asynchronous image loading</a></li>
<li class="level2"><a href="#image-caching">Image caching</a></li>
</ul>
</div>
<h1 class="title">QDeclarativeImageProvider Class Reference</h1>
<!-- $$$QDeclarativeImageProvider-brief -->
<p>The QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML. <a href="#details">More...</a></p>
<!-- @@@QDeclarativeImageProvider -->
<pre class="cpp"> <span class="preprocessor">#include <QDeclarativeImageProvider></span></pre><p>This class was introduced in Qt 4.7.</p>
<ul>
<li><a href="qdeclarativeimageprovider-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-types"></a>
<h2>Public Types</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#ImageType-enum">ImageType</a></b> { Image, Pixmap }</td></tr>
</table>
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#QDeclarativeImageProvider">QDeclarativeImageProvider</a></b> ( ImageType <i>type</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#dtor.QDeclarativeImageProvider">~QDeclarativeImageProvider</a></b> ()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> ImageType </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#imageType">imageType</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QImage </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#requestImage">requestImage</a></b> ( const QString & <i>id</i>, QSize * <i>size</i>, const QSize & <i>requestedSize</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QPixmap </td><td class="memItemRight bottomAlign"><b><a href="qdeclarativeimageprovider.html#requestPixmap">requestPixmap</a></b> ( const QString & <i>id</i>, QSize * <i>size</i>, const QSize & <i>requestedSize</i> )</td></tr>
</table>
<a name="details"></a>
<!-- $$$QDeclarativeImageProvider-description -->
<div class="descr">
<h2>Detailed Description</h2>
<p>The QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML.</p>
<p>QDeclarativeImageProvider is used to provide advanced image loading features in QML applications. It allows images in QML to be:</p>
<ul>
<li>Loaded using QPixmaps rather than actual image files</li>
<li>Loaded asynchronously in a separate thread, if <a href="qdeclarativeimageprovider.html#imageType">imageType</a>() is <a href="qdeclarativeimageprovider.html#ImageType-enum">ImageType::Image</a></li>
</ul>
<p>To specify that an image should be loaded by an image provider, use the <b>"image:"</b> scheme for the URL source of the image, followed by the identifiers of the image provider and the requested image. For example:</p>
<pre class="qml"> <span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a></span> { <span class="name">source</span>: <span class="string">"image://myimageprovider/image.png"</span> }</pre>
<p>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 <a href="qdeclarativeengine.html#addImageProvider">QDeclarativeEngine::addImageProvider</a>().</p>
<p>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").</p>
<pre class="qml"> <span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a></span> { <span class="name">source</span>: <span class="string">"image://MyImageProvider/Image.png"</span> }</pre>
<p>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.</p>
<a name="an-example"></a>
<h4>An example</h4>
<p>Here are two images. Their <tt>source</tt> values indicate they should be loaded by an image provider named "colors", and the images to be loaded are "yellow" and "red", respectively:</p>
<pre class="qml"> <span class="type"><a href="qml-column.html">Column</a></span> {
<span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a></span> { <span class="name">source</span>: <span class="string">"image://colors/yellow"</span> }
<span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a></span> { <span class="name">source</span>: <span class="string">"image://colors/red"</span> }
}</pre>
<p>When these images are loaded by QML, it looks for a matching image provider and calls its <a href="qdeclarativeimageprovider.html#requestImage">requestImage</a>() or <a href="qdeclarativeimageprovider.html#requestPixmap">requestPixmap</a>() method (depending on its <a href="qdeclarativeimageprovider.html#imageType">imageType</a>()) to load the image. The method is called with the <tt>id</tt> parameter set to "yellow" for the first image, and "red" for the second.</p>
<p>Here is an image provider implementation that can load the images requested by the above QML. This implementation dynamically generates <a href="qpixmap.html">QPixmap</a> images that are filled with the requested color:</p>
<pre class="cpp"> <span class="keyword">class</span> ColorImageProvider : <span class="keyword">public</span> <span class="type">QDeclarativeImageProvider</span>
{
<span class="keyword">public</span>:
ColorImageProvider()
: <span class="type">QDeclarativeImageProvider</span>(<span class="type">QDeclarativeImageProvider</span><span class="operator">::</span>Pixmap)
{
}
<span class="type"><a href="qpixmap.html">QPixmap</a></span> requestPixmap(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>id<span class="operator">,</span> <span class="type"><a href="qsize.html">QSize</a></span> <span class="operator">*</span>size<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsize.html">QSize</a></span> <span class="operator">&</span>requestedSize)
{
<span class="type">int</span> width <span class="operator">=</span> <span class="number">100</span>;
<span class="type">int</span> height <span class="operator">=</span> <span class="number">50</span>;
<span class="keyword">if</span> (size)
<span class="operator">*</span>size <span class="operator">=</span> <span class="type"><a href="qsize.html">QSize</a></span>(width<span class="operator">,</span> height);
<span class="type"><a href="qpixmap.html">QPixmap</a></span> pixmap(requestedSize<span class="operator">.</span>width() <span class="operator">></span> <span class="number">0</span> <span class="operator">?</span> requestedSize<span class="operator">.</span>width() : width<span class="operator">,</span>
requestedSize<span class="operator">.</span>height() <span class="operator">></span> <span class="number">0</span> <span class="operator">?</span> requestedSize<span class="operator">.</span>height() : height);
pixmap<span class="operator">.</span>fill(<span class="type"><a href="qcolor.html">QColor</a></span>(id)<span class="operator">.</span>rgba());
<span class="keyword">return</span> pixmap;
}
};</pre>
<p>To make this provider accessible to QML, it is registered with the QML engine with a "colors" identifier:</p>
<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
{
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="type"><a href="qdeclarativeengine.html">QDeclarativeEngine</a></span> engine;
engine<span class="operator">-</span><span class="operator">></span>addImageProvider(QLatin1String(<span class="string">"colors"</span>)<span class="operator">,</span> <span class="keyword">new</span> ColorPixmapProvider);
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
}</pre>
<p>Now the images can be successfully loaded in QML:</p>
<p class="centerAlign"><img src="images/imageprovider.png" alt="" /></p><p>A complete example is available in Qt's <a href="declarative-cppextensions-imageprovider.html">examples/declarative/cppextensions/imageprovider</a> directory. Note the example registers the provider via a <a href="qdeclarativeextensionplugin.html">plugin</a> instead of registering it in the application <tt>main()</tt> function as shown above.</p>
<a name="asynchronous-image-loading"></a>
<h4>Asynchronous image loading</h4>
<p>Image providers that support <a href="qimage.html">QImage</a> loading automatically include support for asychronous loading of images. To enable asynchronous loading for an image source, set the <tt>asynchronous</tt> property to <tt>true</tt> for the relevant <a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a>, <a href="qml-borderimage.html">BorderImage</a> or <a href="qml-animatedimage.html">AnimatedImage</a> 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.</p>
<p>Asynchronous loading is not supported for image providers that provide <a href="qpixmap.html">QPixmap</a> rather than <a href="qimage.html">QImage</a> values, as pixmaps can only be created in the main thread. In this case, if <a href="qml-image.html#asynchronous-prop">asynchronous</a> is set to <tt>true</tt>, the value is ignored and the image is loaded synchronously.</p>
<a name="image-caching"></a>
<h4>Image caching</h4>
<p>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, <a href="qdeclarativeimageprovider.html#requestImage">requestImage</a>() and <a href="qdeclarativeimageprovider.html#requestPixmap">requestPixmap</a>() 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 <tt>cache</tt> property to <tt>false</tt> for the relevant <a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a>, <a href="qml-borderimage.html">BorderImage</a> or <a href="qml-animatedimage.html">AnimatedImage</a> object.</p>
</div>
<p><b>See also </b><a href="qdeclarativeengine.html#addImageProvider">QDeclarativeEngine::addImageProvider</a>().</p>
<!-- @@@QDeclarativeImageProvider -->
<div class="types">
<h2>Member Type Documentation</h2>
<!-- $$$ImageType$$$Image$$$Pixmap -->
<h3 class="fn"><a name="ImageType-enum"></a>enum QDeclarativeImageProvider::<span class="name">ImageType</span></h3>
<p>Defines the type of image supported by this image provider.</p>
<table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><tt>QDeclarativeImageProvider::Image</tt></td><td class="topAlign"><tt>0</tt></td><td class="topAlign">The Image Provider provides <a href="qimage.html">QImage</a> images. The <a href="qdeclarativeimageprovider.html#requestImage">requestImage</a>() method will be called for all image requests.</td></tr>
<tr><td class="topAlign"><tt>QDeclarativeImageProvider::Pixmap</tt></td><td class="topAlign"><tt>1</tt></td><td class="topAlign">The Image Provider provides <a href="qpixmap.html">QPixmap</a> images. The <a href="qdeclarativeimageprovider.html#requestPixmap">requestPixmap</a>() method will be called for all image requests.</td></tr>
</table>
<!-- @@@ImageType -->
</div>
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QDeclarativeImageProvider[overload1]$$$QDeclarativeImageProviderImageType -->
<h3 class="fn"><a name="QDeclarativeImageProvider"></a>QDeclarativeImageProvider::<span class="name">QDeclarativeImageProvider</span> ( <span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">ImageType</a></span> <i>type</i> )</h3>
<p>Creates an image provider that will provide images of the given <i>type</i>.</p>
<!-- @@@QDeclarativeImageProvider -->
<!-- $$$~QDeclarativeImageProvider[overload1]$$$~QDeclarativeImageProvider -->
<h3 class="fn"><a name="dtor.QDeclarativeImageProvider"></a>QDeclarativeImageProvider::<span class="name">~QDeclarativeImageProvider</span> ()<tt> [virtual]</tt></h3>
<p>Destroys the <a href="qdeclarativeimageprovider.html">QDeclarativeImageProvider</a></p>
<p><b>Note:</b> The destructor of your derived class need to be thread safe.</p>
<!-- @@@~QDeclarativeImageProvider -->
<!-- $$$imageType[overload1]$$$imageType -->
<h3 class="fn"><a name="imageType"></a><span class="type"><a href="qdeclarativeimageprovider.html#ImageType-enum">ImageType</a></span> QDeclarativeImageProvider::<span class="name">imageType</span> () const</h3>
<p>Returns the image type supported by this provider.</p>
<!-- @@@imageType -->
<!-- $$$requestImage[overload1]$$$requestImageconstQString&QSize*constQSize& -->
<h3 class="fn"><a name="requestImage"></a><span class="type"><a href="qimage.html">QImage</a></span> QDeclarativeImageProvider::<span class="name">requestImage</span> ( const <span class="type"><a href="qstring.html">QString</a></span> & <i>id</i>, <span class="type"><a href="qsize.html">QSize</a></span> * <i>size</i>, const <span class="type"><a href="qsize.html">QSize</a></span> & <i>requestedSize</i> )<tt> [virtual]</tt></h3>
<p>Implement this method to return the image with <i>id</i>. The default implementation returns an empty image.</p>
<p>The <i>id</i> is the requested image source, with the "image:" scheme and provider identifier removed. For example, if the image <a href="qml-image.html#source-prop">source</a> was "image://myprovider/icons/home", the given <i>id</i> would be "icons/home".</p>
<p>The <i>requestedSize</i> corresponds to the <a href="qml-image.html#sourceSize-prop">Image::sourceSize</a> requested by an Image element. If <i>requestedSize</i> is a valid size, the image returned should be of that size.</p>
<p>In all cases, <i>size</i> must be set to the original size of the image. This is used to set the <a href="qml-item.html#width-prop">width</a> and <a href="qml-item.html#height-prop">height</a> of the relevant <a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a> if these values have not been set explicitly.</p>
<p><b>Note:</b> this method may be called by multiple threads, so ensure the implementation of this method is reentrant.</p>
<!-- @@@requestImage -->
<!-- $$$requestPixmap[overload1]$$$requestPixmapconstQString&QSize*constQSize& -->
<h3 class="fn"><a name="requestPixmap"></a><span class="type"><a href="qpixmap.html">QPixmap</a></span> QDeclarativeImageProvider::<span class="name">requestPixmap</span> ( const <span class="type"><a href="qstring.html">QString</a></span> & <i>id</i>, <span class="type"><a href="qsize.html">QSize</a></span> * <i>size</i>, const <span class="type"><a href="qsize.html">QSize</a></span> & <i>requestedSize</i> )<tt> [virtual]</tt></h3>
<p>Implement this method to return the pixmap with <i>id</i>. The default implementation returns an empty pixmap.</p>
<p>The <i>id</i> is the requested image source, with the "image:" scheme and provider identifier removed. For example, if the image <a href="qml-image.html#source-prop">source</a> was "image://myprovider/icons/home", the given <i>id</i> would be "icons/home".</p>
<p>The <i>requestedSize</i> corresponds to the <a href="qml-image.html#sourceSize-prop">Image::sourceSize</a> requested by an Image element. If <i>requestedSize</i> is a valid size, the image returned should be of that size.</p>
<p>In all cases, <i>size</i> must be set to the original size of the image. This is used to set the <a href="qml-item.html#width-prop">width</a> and <a href="qml-item.html#height-prop">height</a> of the relevant <a href="qdeclarativeimageprovider.html#ImageType-enum">Image</a> if these values have not been set explicitly.</p>
<!-- @@@requestPixmap -->
</div>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|