qt(vs2010) 手动配置moc生成规则

Visual Studio 2010 中写QT项目时,有时需要 手动配置 MOC 生成规则


操作步骤

  1. 右键 .h 文件 → Properties
  2. Configuration Properties > Custom Build Tool 中:
    • Command Line:
      "$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\moc_%(Filename).cpp"
      
    • Outputs:
      .\GeneratedFiles\moc_%(Filename).cpp
      
  3. 手动将生成的 moc_*.cpp 文件添加到项目(GeneratedFiles 目录是否存在 moc_*.cpp 文件)。

常见问题

(1)生成的 moc_*.cpp 未编译
  • 原因:文件未被包含在项目中或标记为“排除生成”。
  • 解决
    • Solution Explorer 中右键 GeneratedFilesInclude In Project
    • 检查文件属性中的 Excluded From Build 是否为 No
(2)路径或环境变量错误
  • 确保
    • QTDIR 环境变量指向 Qt 4.8.6 安装目录(如 C:\Qt\4.8.6)。
    • moc.exe 路径正确(默认在 $(QTDIR)\bin)。

你可能感兴趣的:(C++/QT,qt,数据库,开发语言)