QT手动moc问题:virtual struct QMetaObject const * __thiscall Widget::metaObject

来源:互联网 发布:屏幕视频截取软件 编辑:程序博客网 时间:2024/06/05 14:54

【转自】http://blog.csdn.net/zhenyusoso/article/details/8450906

编写一个简单的给予QT的DEMO程序步骤大致如下:
下面开始编程:
A 编写 main.cpp
B 编写 hello.h
C 编写 hello.cpp
D 制作.ui文件,并生成ui_hello.h
打开designer,拖入一个pushButton,一个label。保存为hello.ui。然后生成ui_hello.h,并添加到项目的源文件中。

编译运行,则出现如下错误:
错误 1 error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject(void)const " (?metaObject@Widget@@UBEPBUQMetaObject@@XZ) 
错误 2 error LNK2001: 无法解析的外部符号 "public: virtual void * __thiscall Widget::qt_metacast(char const *)" (?qt_metacast@Widget@@UAEPAXPBD@Z) 
错误 3 error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall Widget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Widget@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
错误 4 fatal error LNK1120: 3 个无法解析的外部命令  
   
这是因为在源文件中没有添加上moc_hello.cpp文件。
解决方法:右击hello.h,选择“自定义生成步骤”,“常规”
命令行:moc.exe hello.h -o moc_hello.cpp
输出:moc_hello.cpp
附加依赖项:moc.exe hello.h
确定,然后,右击hello.h,选择 “编译”,则在文件夹中生成moc_hello.cpp,再将其添加到源文件中。

0 0