Qt Quick Shared Image Provider
摘要
在多个Qt Quick 应用程序使用相同本地图像文件的情况下,该模块提供了节省内存的功能。它将解码后的QImage 数据放在共享系统内存中,使所有进程都能访问(参见QSharedMemory )。
该模块只共享 CPU 内存。它不提供 GPU 内存或纹理共享。
使用方法
要使用该模块,请像这样导入:
import Qt.labs.sharedimage
共享功能是通过QQuickImageProvider 提供的。使用 "image:"(图像:)方案来表示图像的 URL 源,然后是标识符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 类型。
© 2025 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.