qt(vs2010) 手动配置moc生成规则
在 Visual Studio 2010 中写QT项目时,有时需要 手动配置 MOC 生成规则
操作步骤:
- 右键
.h
文件 →Properties
。 - 在
Configuration Properties > Custom Build Tool
中:- Command Line:
"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\moc_%(Filename).cpp"
- Outputs:
.\GeneratedFiles\moc_%(Filename).cpp
- Command Line:
- 手动将生成的
moc_*.cpp
文件添加到项目(GeneratedFiles
目录是否存在moc_*.cpp
文件)。
常见问题
(1)生成的 moc_*.cpp
未编译
- 原因:文件未被包含在项目中或标记为“排除生成”。
- 解决:
- 在
Solution Explorer
中右键GeneratedFiles
→Include In Project
。 - 检查文件属性中的
Excluded From Build
是否为No
。
- 在
(2)路径或环境变量错误
- 确保:
QTDIR
环境变量指向 Qt 4.8.6 安装目录(如C:\Qt\4.8.6
)。moc.exe
路径正确(默认在$(QTDIR)\bin
)。