Class: QgsImageCache¶
- class qgis.core.QgsImageCache(parent: QObject = None)¶
Bases:
QgsAbstractContentCacheBase
Constructor for
QgsImageCache
, with the specifiedparent
object.A cache for images derived from raster files.
QgsImageCache
stores pre-rendered resampled versions of raster image files, allowing efficient reuse without incurring the cost of resampling on every render.QgsImageCache
is not usually directly created, but rather accessed throughQgsApplication.imageCache()
.Methods
Runs additional checks on a network
reply
to ensure that the reply content is consistent with that required by the cache.Triggered after remote content (i.e.
Returns the original size (in pixels) of the image at the specified
path
.Returns the specified
path
rendered as an image.Signals
Emitted when the cache has finished retrieving an image file from a remote
url
.- checkReply(self, reply: QNetworkReply, path: str) → bool¶
Runs additional checks on a network
reply
to ensure that the reply content is consistent with that required by the cache.
- childEvent(self, QChildEvent)¶
- connectNotify(self, QMetaMethod)¶
- customEvent(self, QEvent)¶
- disconnectNotify(self, QMetaMethod)¶
- isSignalConnected(self, QMetaMethod) → bool¶
- onRemoteContentFetched(self, url: str, success: bool)¶
Triggered after remote content (i.e. HTTP linked content at the given
url
) has been fetched.The
success
argument will beTrue
if the content was successfully fetched, orFalse
if it was not fetched successfully.
- originalSize(self, path: str, blocking: bool = False) → QSize¶
Returns the original size (in pixels) of the image at the specified
path
.path
may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).If
path
is a remote file, then an invalid size may be returned while the image is in the process of being fetched.The
blocking
boolean forces to wait for loading before returning the original size. The content is loaded in the same thread to ensure provided the original size. WARNING: theblocking
parameter must NEVER beTrue
from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.If the image could not be read then an invalid QSize is returned.
- Parameters
path (str) –
blocking (bool = False) –
- Return type
QSize
- pathAsImage(self, path: str, size: QSize, keepAspectRatio: bool, opacity: float, blocking: bool = False) → Tuple[QImage, bool]¶
Returns the specified
path
rendered as an image. If possible, a pre-existing cached version of the image will be used. If not, the image is fetched and resampled to the desired size, and then the result cached for subsequent lookups.path
may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).The
size
parameter dictates the target size of the image. An invalid size indicates the original raster image size (with no resampling). A size in which the width or height is set to zero will have the zeroed value automatically computed when keepAspectRatio isTrue
.If
keepAspectRatio
isTrue
, then the original raster aspect ratio will be maintained during any resampling operations.An
opacity
parameter dictates the opacity of the image.If the resultant raster was of a sufficiently small size to store in the cache, then
fitsInCache
will be set toTrue
.The
blocking
boolean forces to wait for loading before returning image. The content is loaded in the same thread to ensure provided the image. WARNING: theblocking
parameter must NEVER beTrue
from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.- Parameters
path (str) –
size (QSize) –
keepAspectRatio (bool) –
opacity (float) –
blocking (bool = False) –
- Return type
Tuple[QImage, bool]
- receivers(self, PYQT_SIGNAL) → int¶
- remoteImageFetched¶
Emitted when the cache has finished retrieving an image file from a remote
url
. [signal]- Parameters
url (str) –
- sender(self) → QObject¶
- senderSignalIndex(self) → int¶
- timerEvent(self, QTimerEvent)¶