您的位置:首页 > 编程语言 > Qt开发

Qt中moc问题(qt moc 处理 cpp)

2015-11-27 14:54 375 查看
我用的是QT Designer,一般只有用到信号signals和槽slots时才会用到MOC,因为采用信号signals和槽slots是QT的特性,而C++没有,所以采用了MOC(元对象编译器)把信号signals和槽slots部分编译成C++语言.

用信号signals和槽slots需注意的基本问题是:

1)在类class声明中必须加入Q_OBJECT;

2)在CPP文件中要把信号signals和槽slots联系起来,即使用connect,例connect( iv, SIGNAL(clicked (QIconViewItem *)), this, SLOT( draw()));

至于MOC处理QT Designer一般都自行处理,我觉得有些书籍写关于MOC方面的东东纯粹就是乱搞,读者思路本清晰,却有时被它搞得思维混乱!!!

再次强调:只要按上述方式就行了,因为MOC文件的生成和继承都是自动的!!!

在使用qt的时候,常常为了实现的需求将一些类隐藏在cpp中文件实现,而这些类又需要一些qt自己的机制支持如Q_OBJECT宏。于是在编译的时候,很可能出现像undefined
reference to vtable for "xxx::xxx"的问题,这其实是由于qt不会自动moc cpp文件。参考qt的文档,发现最简单的方法就是用qmake重新生成makefile文件就可以了。另外就是直接把该类写在.h文件里面在编译。

如果需要自己重新写makefile,qt文档给出了以下的方法

For Q_OBJECT class declarations in header files, here is a useful makefile rule if you only use GNU make:

moc_%.cpp: %.h

moc $(DEFINES) $(INCPATH) $< -o $@

If you want to write portably, you can use individual rules of the following form:

moc_foo.cpp: foo.h

moc $(DEFINES) $(INCPATH) $< -o $@

You must also remember to add moc_foo.cpp to your SOURCES (substitute your favorite name) variable and moc_foo.o or moc_foo.obj to your OBJECTS variable.

Both examples assume that $(DEFINES) and $(INCPATH) expand to the define and include path options that are passed to the C++ compiler. These are required by moc to preprocess the source files.

While we prefer to name our C++ source files .cpp, you can use any other extension, such as .C, .cc, .CC, .cxx, and .c++, if you prefer.

For Q_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:

foo.o: foo.moc

foo.moc: foo.cpp

moc $(DEFINES) $(INCPATH) -i $< -o $@

This guarantees that make will run the moc before it compiles foo.cpp. You can then put

#include "foo.moc"

对于.cpp文件里面的Q_OBJECT,我们只需在makefile文件里面添加

foo.o: foo.moc

foo.moc: foo.cpp

moc $(DEFINES) $(INCPATH) -i $< -o $@

这样的规则就可以了,然后在.cpp文件的末尾写上

#include "foo.moc"

这样就能够正确的编译,不过我最推荐的还是用qmake重新生成makefile。

另外,对于qtopia,我们使用qtopiamake重新生成makefile,但是我在qtopiamake的时候出现了一点问题,就是重新编译的时候还是出错,不过如果首先make
clean,然后在删除makefile,在使用qtopiamake,然后在make就不会出错。而我在qt下就直接用qmake后make,不会出错。可能是因为qtopiamake并没有删除原来生成的.obj文件的原因吧。

原帖:http://blog.sina.com.cn/s/blog_607142c90100lgz6.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: