<QtResource>

用于管理 Qt 资源系统的宏。更多内容...

Header: #include <QtResource>

宏

void Q_CLEANUP_RESOURCE(name)
void Q_INIT_RESOURCE(name)

详细说明

宏文档

void Q_CLEANUP_RESOURCE(name)

卸载由.qrc 文件指定的、基础名称为name 的资源。

通常情况下,Qt 资源会在应用程序终止时自动卸载,但如果资源位于正在被卸载的插件中,请调用 Q_CLEANUP_RESOURCE() 来强制卸载您的资源。

注意:此 宏不能在命名空间中使用。请参阅Q_INIT_RESOURCE 文档以了解解决方法。

示例:

Q_CLEANUP_RESOURCE(myapp);

另请参阅 Q_INIT_RESOURCE() 和《Qt 资源系统》。

void Q_INIT_RESOURCE(name)

使用指定的基类name 初始化由.qrc 文件指定的资源。通常,当资源作为应用程序的一部分构建时,这些资源会在启动时自动加载。在某些平台上,对于存储在静态库中的资源,必须使用 Q_INIT_RESOURCE() 宏。

例如,如果应用程序的资源列在名为myapp.qrc 的文件中,您可以通过在main() 函数中添加以下这行代码,确保资源在启动时被初始化:

Q_INIT_RESOURCE(myapp);

如果文件名中包含不能作为有效 C++ 函数名组成部分的字符(例如 '-'),则必须将其替换为下划线字符 ('_')。

注意:此 宏不能在命名空间中使用。应从 main() 函数中调用它。如果无法做到这一点,可以使用以下变通方法,从MyNamespace::myFunction 函数中初始化资源myapp :

inline void initMyResource() { Q_INIT_RESOURCE(myapp); }

namespace MyNamespace
{
    //...

    void myFunction()
    {
        initMyResource();
    }
}

另请参阅 Q_CLEANUP_RESOURCE() 和《Qt 资源系统》。

© 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.