https://techbase.kde.org/api.php?action=feedcontributions&user=Thunderchild&feedformat=atomKDE TechBase - User contributions [en]2024-03-28T12:11:14ZUser contributionsMediaWiki 1.40.2https://techbase.kde.org/index.php?title=Development/Tutorials/KPixmapCache&diff=53054Development/Tutorials/KPixmapCache2010-08-01T19:08:10Z<p>Thunderchild: /* Creating the cache object */</p>
<hr />
<div>{{Template:I18n/Language Navigation Bar|Development/Tutorials/KPixmapCache}}<br />
== Introduction ==<br />
{{class|KPixmapCache}} provides disk-caching of QPixmaps. If you're using SVGs or generating pixmaps from some data, you might consider using it as it eliminates the need to generate the same pixmaps over and over again.<br />
<br />
== Using it ==<br />
=== Creating the cache object ===<br />
Its API is similar to that of the QPixmapCache.<br />
To use it, you first need to create the cache object, using a unique name:<br />
<code cppqt><br />
KPixmapCache* cache = new KPixmapCache("myapp-images");<br />
</code><br />
<br />
=== Inserting and finding pixmaps ===<br />
Once that is done you can easily retrieve and insert images with insert() and find() methods:<br />
<code cppqt><br />
QPixmap pix("file1.png");<br />
cache->insert("file1.png", pix);<br />
<br />
QPixmap pix2;<br />
if (cache->find("file2.png", pix2)) {<br />
// The pixmap was loaded from cache.<br />
} else {<br />
// Pixmap was not found in the cache.<br />
// Load and insert it:<br />
pix2 = QPixmap("file2.png");<br />
cache->insert("file2.png", pix2);<br />
}<br />
</code><br />
<br />
=== The Easy Way ===<br />
As loading pixmaps from files with the aid of cache is a very common operation, KPixmapCache provides a convenience method for that:<br />
<code cppqt><br />
QString filename("myfile.png");<br />
QPixmap pix = cache->loadFromFile(filename);<br />
</code><br />
This first checks if the given file is in the cache. If it is, it's loaded from the cache, otherwise it's loaded from the file and inserted into cache.<br />
<br />
Similar method is provided for loading SVG files:<br />
<code cppqt><br />
QString filename("myfile.svg");<br />
QPixmap pix = cache->loadFromSvg(filename);<br />
</code><br />
This renders contents of the SVG file onto pixmap, using SVG's default size by default. If you need a different size, you can specify it as the second argument of that method:<br />
<code cppqt><br />
QString filename("myfile.svg");<br />
QSize size(150, 250);<br />
QPixmap pix = cache->loadFromSvg(filename, size);<br />
</code><br />
This renders the SVG onto a pixmap with the size of 150x250 pixels.<br />
<br />
<br />
=== Timestamps ===<br />
Often you need to make sure the cache is up-to-date and you're not using obsolete pixmaps. For this, the cache provides timestamp() method. By default it returns time when the cache was created, but you can set your own timestamp with setTimestamp() method.<br />
<br />
<code cppqt><br />
// Our data<br />
MyDataObject data;<br />
// Make sure the cache is up to date<br />
if (cache->timestamp() < data.timestamp()) {<br />
// Data is newer than the cache<br />
// Thus the cache is obsolete, delete and reinitialize it<br />
cache->discard();<br />
}<br />
// Here the cache is always recent enough<br />
QPixmap visualizedData;<br />
QString key("data-visualization");<br />
// Try to find the visualization from the cache<br />
if (!cache->find(key, visualizedData)) {<br />
// Visualization is not in the cache, recreate it...<br />
visualizedData = createVisualization(data);<br />
// ...and put the resulting image into cache<br />
cache->insert(key, visualizedData);<br />
}<br />
// Show the visualizedData pixmap to user<br />
</code><br />
This example creates a visual representation of some data. If the data is more recent than the cache, then contents of the cache are deleted using the discard() method. Next, we check if cache already contains the necessary pixmap. If it does, then there is no need to recreate it from the data (which might be expensive).<br />
<br />
=== API docs ===<br />
For a list of all KPixmapCache methods, check out its [http://api.kde.org/classmapper.php?class=KPixmapCache&module=kdelibs&version=4.0 API docs].</div>Thunderchild