博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于添加Q_OBJECT后引发的错误

Posted on 2012-12-28 10:18  星尘的天空  阅读(2009)  评论(0编辑  收藏  举报

关于添加Q_OBJECT后引发的错误

Q_OBJECT 

:The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots.

在QGIS中,对于那些在类中需要添加 信号槽的类,就需要在该类定义的时候的添加 Q_OBJECT这个宏命令,这个是必须的

To compile the program, run qmake as usual. Since the FindDialog class definition contains the Q_OBJECT macro, the makefile generated by qmake will include special rules to run moc, Qt's meta-object compiler. (We cover Qt's meta-object system in the next section.)

For moc to work correctly, we must put the class definition in a header file, separate from the implementation file. The code generated by moc includes this header file and adds some C++ boilerplate code of its own.

Classes that use the Q_OBJECT macro must have moc run on them. This isn't a problem because qmake automatically adds the necessary rules to the makefile. But if you forget to regenerate your makefile using qmake and moc isn't run, the linker will complain that some functions are declared but not implemented. The messages can be fairly obscure. GCC produces error messages like this one:

finddialog.o: In function `FindDialog::tr(char const*, char const*)':
/usr/lib/qt/src/corelib/global/qglobal.h:1430: undefined reference to
`FindDialog::staticMetaObject'

 

Visual C++'s output starts like this:

finddialog.obj : error LNK2001: unresolved external symbol
"public:~virtual int __thiscall MyClass::qt_metacall(enum QMetaObject
::Call,int,void * *)"

 

If this ever happens to you, run qmake again to update the makefile, then rebuild the application.

 

我使用的VS 2008,但是在实际的操作过程中,尽管我按照上面的命令重新编译了上面的文件,单是仍然会报错,经过反复测试,我发现,添加一个带有Q_OBJECT的类后,在执行上述命令后,需要将vs的解决方案文件删除掉,然后在运行上面的命令,重新生成.sln解决方案,然后重新打开工程,这个问题就解决了,虽然这个方法比较笨蛋,有点麻烦,但在好的方法发现之前,这仍是一个解决问题的方法。

 

THE END!

2012年12月28日