本页内容

Qt Quick Shared Image Provider

摘要

本模块提供了一项功能,可在多个Qt Quick 应用程序使用相同的本地图像文件时节省内存。其实现方式是将解码后的QImage 数据放置在共享的系统内存中,从而使所有进程都能访问该数据(参见QSharedMemory )。

该模块仅共享CPU内存,不支持GPU内存或纹理的共享。

用法

要使用此模块,请按以下方式导入:

import Qt.labs.sharedimage

共享功能通过QQuickImageProvider 提供。在图像URL源中使用“image:”方案,后跟标识符shared,再后跟图像文件路径。例如:

Image { source: "image://shared/usr/share/wallpapers/mybackground.jpg" }

这将查找文件/usr/share/wallpapers/mybackground.jpg。第一个执行此操作的进程将通过常规的 Qt 图像加载方式读取该图像文件。随后,解码后的图像数据将作为共享内存中的数据,并以完整的文件路径作为键。 后续请求同一图像的进程将发现数据已存在于共享内存中,因此会直接使用该数据,而无需再次加载图像文件。

共享的图像数据将一直保持可用,直到最后一个进程删除了其对共享图像的最后一个引用,此时该数据才会被自动释放。

如果系统不支持内存共享,共享图像提供程序将回退到常规的、非共享的图像加载方式。

文件路径必须是绝对路径。若要使用相对路径,请通过Qt.resolvedUrl()将其转换为绝对路径,并替换 URL 方案。例如:

...
property string imagePrefix: Qt.resolvedUrl("../myimages/").replace("file://", "image://shared/")
Image { source: imagePrefix + "myimage.png" }

共享图像模块不提供任何可直接使用的 QML 类型。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.