在此页面上

管理资源

向 Qt 项目添加新资源与向普通 C++ 项目添加资源类似。主要区别在于,您使用的是 Qt 资源文件 (.qrc ) 而不是 Windows.rc 文件。与.rc 文件不同,.qrc 文件可在 Qt 支持的所有平台上运行,而且从 Qt 代码中加载也很简单。

添加新资源

  1. 转到Project >Add New Item >Installed >Visual C++ >Qt 。
  2. 选择Qt Resource File 。

    添加新项目对话框

  3. 在Name 中,输入资源文件的名称。
  4. 在Location 中,指定文件的位置。
  5. 选择Add 可创建.qrc 文件,并在Qt Resource Editor 中打开。

    Qt 资源编辑器

  6. 要向文件中添加资源,请选择Add >Add Files 。
  7. 在Prefix 中,可以更改前缀。

以后在 Qt 代码中引用资源时,必须在文件名前加上前缀和冒号。例如,:/MyProject/print.bmp 。

添加前缀

要在.qrc 文件中添加前缀,请在Qt Resource Editor 中选择Add >Add Prefix 。

另请参阅 启动 Qt 资源编辑器和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.