Q_OBJECT与link error(转)

本文详细介绍了如何在Qt项目中使用MOC工具生成所需的元对象代码,包括配置MOC工具、为继承自Qt类的头文件生成MOC文件及将这些文件添加到项目中的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为没有moc,信号和插槽机制,必须定义Q_OBJECT宏,标准c++没有对Qt的元对象系统所需要的动态元信息提供支持,qt提供了moc工具解决这个问题。
建议大家把解决的问题的方法分享一下。
GENERATE MOC FILES FOR HEADERS USING QT EVENTS (Q_OBJECT):
Perform this step for all header files that inherit from a QT class - i.e. any class with Q_OBJECT.  The following example uses a ficticious header file called MyHeaderFile.h You must fully qualify where the header file is or the moc utility will not find it.

In the Solution Explorer, right Click on MyHeaderFile.h and select Properties -> Custom Build Step -> General  
Set Command Line to $(QTDIR)/bin/moc (路径/)MyHeaderFile.h -o tmp/moc/moc_MyHeaderFile.cpp
Set Description to Moc'ing MyHeaderFile.h ...
Set Outputs to tmp/moc/moc_MyHeaderFile.cpp
Set Additional Dependences $(QTDIR)/bin/moc.exe;
Replace MyHeaderFile with the actual name of the header file to be Moc'd.  

Screenshot8.gif
Screenshot9.gif  


7) ADD THE MOC FILES TO YOUR PROJECT:
First, build your project by selecting Build -> Build Solution or (F7) to generate moc files (you'll get a bunch of linking errors - don't worry).

From the Solution Explorer, right click on the  Project_Name and select Add -> New Folder
Name the new folder "Generated Moc Files" (or whatever you want).

Again, from the Solution Explorer right click on the "Generated Moc Files" folder and select Add -> Add Existing Item

Lastly, locate the moc file from the directory specified in step 6 (i.e. tmp/moc/moc_MyHeaderFile.cpp) to add it to your project.

That's it, you should be up and running.  Good Luck.

(转自:http://www.kangq.mobi/bbs/read.php?tid=15939

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值