Q_OBJECT与link error(转)

因为没有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)

你可能感兴趣的:(Q_OBJECT与link error(转))