rcc 是 Qt Resource Compiler 的缩写,是 Qt 资源系统的核心工具。它负责将资源文件(如图片、QML 文件、音频等)编译成 C++ 代码或二进制格式,嵌入到应用程序中。
安装与位置
bash
# 在 Ubuntu/Debian 中安装
sudo apt install qtbase5-dev-tools
# 查看 rcc 位置
which rcc
# 通常位于:/usr/lib/qt5/bin/rcc 或 /usr/bin/rcc
# 查看版本
rcc -version
# 或
rcc -v
基本用法
1. 资源文件格式 (.qrc)
首先需要一个 XML 格式的资源描述文件:
XML
<!-- resources.qrc -->
<RCC>
<qresource prefix="/images">
<file>icon.png</file>
<file>logo.svg</file>
<file alias="app_icon">app/icon.ico</file>
</qresource>
<qresource prefix="/qml" lang="en">
<file>main.qml</file>
<file>components/Button.qml</file>
</qresource>
<qresource prefix="/sounds" compressed="true">
<file>click.wav</file>
</qresource>
</RCC>
2. 编译资源文件
bash
# 基本用法:生成 C++ 源文件
rcc -name myresources resources.qrc -o qrc_myresources.cpp
# 指定压缩级别(1-9,9为最高压缩)
rcc -name myresources resources.qrc --compress 9 -o qrc_myresources.cpp
# 禁用压缩
rcc -name myresources resources.qrc --no-compress -o qrc_myresources.cpp
# 指定压缩阈值(大于该值的文件才压缩,单位KB)
rcc -name myresources resources.qrc --threshold 3 -o qrc_myresources.cpp
# 生成二进制 .rcc 文件
rcc -binary resources.qrc -o myresources.rcc
# 从标准输入读取
cat resources.qrc | rcc -name myresources -o qrc_myresources.cpp
高级功能
1. 条件编译
XML
<!-- 支持条件编译,根据 Qt 版本或平台包含不同资源 -->
<RCC>
<qresource prefix="/">
<file>common.png</file>
<file condition="QT_VERSION >= QT_VERSION_CHECK(5,15,0)">new_feature.qml</file>
<file condition="Q_OS_WIN">windows/icon.ico</file>
<file condition="Q_OS_LINUX">linux/icon.png</file>
<file condition="Q_OS_MAC">mac/icon.icns</file>
</qresource>
</RCC>
2. 多语言资源
XML
<RCC>
<qresource prefix="/translations">
<file>app_en_US.qm</file>
<file>app_zh_CN.qm</file>
</qresource>
<qresource prefix="/images" lang="en">
<file>en/flag.png</file>
</qresource>
<qresource prefix="/images" lang="zh">
<file>zh/flag.png</file>
</qresource>
</RCC>
在 Qt 项目中使用
QMake 项目 (.pro 文件)
bash
# 自动方式:Qt 自动处理 .qrc 文件
RESOURCES += \
resources.qrc \
images.qrc
# 手动方式:自定义 rcc 参数
system(rcc -name myapp resources.qrc -o $${OUT_PWD}/qrc_resources.cpp)
SOURCES += $${OUT_PWD}/qrc_resources.cpp
# 条件编译资源
win32 {
RESOURCES += windows_resources.qrc
}
unix {
RESOURCES += unix_resources.qrc
}
CMake 项目 (CMakeLists.txt)
bash
# 自动方式:使用 Qt 的 CMake 函数
qt_add_resources(app_resources "resources"
PREFIX "/"
FILES
images/icon.png
qml/main.qml
)
# 或从 .qrc 文件添加
qt_add_resources(app_resources "resources"
PREFIX "/"
BASE resources.qrc
)
# 链接到目标
target_link_libraries(myapp PRIVATE app_resources)
# 手动方式
add_custom_command(
OUTPUT qrc_resources.cpp
COMMAND ${Qt5Core_RCC_EXECUTABLE}
ARGS -name resources resources.qrc -o qrc_resources.cpp
MAIN_DEPENDENCY resources.qrc
)
target_sources(myapp PRIVATE qrc_resources.cpp)
运行时使用资源
C++ 中访问资源
cpp
// 1. 使用资源路径(编译后路径)
QIcon icon(":/images/icon.png");
QPixmap pixmap(":/images/logo.svg");
// 2. 使用 QResource 类
QResource resource(":/data/config.json");
QByteArray data = resource.uncompressedData();
// 3. 检查资源是否存在
bool exists = QFile::exists(":/scripts/init.js");
// 4. 列出资源目录
QDir resourceDir(":/");
QStringList files = resourceDir.entryList();
// 5. 动态加载 .rcc 文件
QResource::registerResource("extra_resources.rcc");
QResource::unregisterResource("extra_resources.rcc");
QML 中访问资源
javascript
// QML 中直接使用资源路径
Image {
source: "qrc:/images/background.png"
}
// 使用 Qt.resolvedUrl
property string imagePath: Qt.resolvedUrl("qrc:/images/icon.png")
// 字体资源
FontLoader {
source: "qrc:/fonts/Roboto-Regular.ttf"
}
实际示例
示例 1:完整项目结构
bash
myapp/
├── CMakeLists.txt
├── src/
│ ├── main.cpp
│ └── resources.qrc
├── images/
│ ├── icon.png
│ └── logo.svg
├── qml/
│ ├── main.qml
│ └── components/
│ └── Button.qml
└── translations/
├── app_en_US.ts
└── app_zh_CN.ts
示例 2:构建脚本
bash
#!/bin/bash
# build_resources.sh
# 清理旧文件
rm -f qrc_*.cpp *.rcc
# 编译主资源
rcc -name app_resources resources.qrc \
--compress 9 \
--threshold 100 \
-o qrc_app_resources.cpp
# 编译语言资源
rcc -binary translations.qrc -o translations.rcc
# 编译主题资源(条件编译)
if [ "$THEME" = "dark" ]; then
rcc -name theme theme_dark.qrc -o qrc_theme.cpp
else
rcc -name theme theme_light.qrc -o qrc_theme.cpp
fi
echo "资源编译完成"
示例 3:Python 集成
bash
#!/usr/bin/env python3
# 使用 subprocess 调用 rcc
import subprocess
import os
def compile_qrc(qrc_file, output_cpp, resource_name="resources"):
"""编译 .qrc 文件为 C++ 源文件"""
cmd = [
"rcc",
"-name", resource_name,
qrc_file,
"-o", output_cpp,
"--compress", "9"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"错误: {result.stderr}")
return False
print(f"成功编译 {qrc_file} -> {output_cpp}")
return True
# 使用示例
compile_qrc("resources.qrc", "qrc_resources.cpp")
性能优化技巧
1. 压缩策略
bash
# 小文件不压缩(减少 CPU 开销)
rcc --threshold 10 # 只压缩大于 10KB 的文件
# 平衡压缩比和性能
rcc --compress 6 # 中等压缩级别
# 图片已压缩,无需再次压缩
rcc --no-compress images.qrc
2. 资源分组
bash
# 按类型分组,提高加载效率
rcc -name images images.qrc -o qrc_images.cpp
rcc -name qml qml.qrc -o qrc_qml.cpp
rcc -name data data.qrc -o qrc_data.cpp
# 按使用频率分组
rcc -name startup startup.qrc # 启动时需要的资源
rcc -name runtime runtime.qrc # 运行时加载的资源
3. 延迟加载
cpp
// 动态加载 .rcc 文件,减少启动时间
void loadResourcesWhenNeeded() {
static bool loaded = false;
if (!loaded) {
QResource::registerResource("lazy_resources.rcc");
loaded = true;
}
}
调试与排查
查看资源内容
bash
# 列出 .rcc 文件内容
rcc --list myresources.rcc
# 详细输出编译信息
rcc -verbose resources.qrc -o qrc_resources.cpp
# 输出依赖关系
rcc --dependencies resources.qrc
# 检查资源文件是否存在
rcc --check resources.qrc
调试技巧
cpp
// 1. 检查资源是否注册
qDebug() << "Resource exists:" << QFile::exists(":/images/icon.png");
// 2. 列出所有资源
QDirIterator it(":", QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}
// 3. 获取资源信息
QResource res(":/data/file.txt");
qDebug() << "Size:" << res.size();
qDebug() << "Compressed:" << res.isCompressed();
qDebug() << "Last modified:" << res.lastModified();
跨平台注意事项
路径处理
bash
# 使用相对路径(推荐)
rcc -name resources ../resources/resources.qrc -o qrc_resources.cpp
# 处理 Windows 反斜杠
rcc -name resources "C:\\project\\resources.qrc" -o qrc_resources.cpp
# 使用环境变量
rcc -name resources ${RESOURCE_PATH}/resources.qrc -o qrc_resources.cpp
文件系统大小写
bash
# Linux 区分大小写,确保路径正确
rcc -name resources Resources.qrc # 可能失败
rcc -name resources resources.qrc # 正确
最佳实践
资源命名规范
bash
# 好:清晰的前缀
rcc -name app_images
rcc -name app_sounds
# 不好:模糊的命名
rcc -name data1
按需编译
bash
# 开发环境:快速编译,低压缩
rcc --no-compress --threshold 0
# 生产环境:高压缩
rcc --compress 9 --threshold 1
版本控制
bash
# 不提交生成的 .cpp/.rcc 文件
# 只提交 .qrc 和原始资源文件
# .gitignore 示例
qrc_*.cpp
*.rcc
自动化集成
bash
# 在 CMake/QMake 中自动处理
# 或使用构建脚本
# 或集成到 CI/CD 流程
常见问题解决
问题 1:资源找不到
bash
# 检查 .qrc 文件路径
# 确保文件在 .qrc 指定的相对路径中
# 使用绝对路径测试
rcc -name test /absolute/path/to/resources.qrc -o test.cpp
问题 2:内存占用高
bash
# 减少压缩级别
rcc --compress 1
# 增大压缩阈值
rcc --threshold 1000 # 只压缩大于 1MB 的文件
# 分割资源文件
# 将大资源单独编译,动态加载
问题 3:编译速度慢
bash
# 并行编译多个资源文件
make -j4
# 增量编译:只修改必要的 .qrc 文件
# 使用 ccache 缓存编译结果
通过合理使用 rcc,可以有效地管理 Qt 应用程序的资源,提高应用程序的部署便利性和运行效率。