代码如下:
[doyle@phuang Q_INVOKABLE]$ cat q_invokable.h
#include <QObject>
#include <QMetaObject>
#include <QDebug>
namespace Type{
class Test : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE Test(): QObject( 0 ), _x( 0 ) {}
Q_INVOKABLE void setX( int i ) { _x = i; }
int foo() { return _x; }
private:
int _x;
};
};
[doyle@phuang Q_INVOKABLE]$ cat test_q_invokable.cpp
#include <QCoreApplication>
#include "q_invokable.h"
using namespace Type;
int main( int argc, char **argv )
{
QCoreApplication app( argc, argv );
const QMetaObject * obj = &(Test::staticMetaObject);
Test *t = qobject_cast<Test *>(obj->newInstance());
qDebug() << "foo = " << t->foo();
QMetaObject::invokeMethod(t, "setX", Q_ARG( int, 42 ) );
qDebug() << "foo = " << t->foo();
return 0;
}
[doyle@phuang Q_INVOKABLE]$ make
g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i586 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o moc_q_invokable.o moc_q_invokable.cpp
moc_q_invokable.cpp: In function 'int Type__Test_qt_static_metacall(QMetaObject::Call, int, void**)':
moc_q_invokable.cpp:48: error: 'Test' was not declared in this scope
moc_q_invokable.cpp:48: error: '_r' was not declared in this scope
moc_q_invokable.cpp:48: error: expected type-specifier before 'Test'
moc_q_invokable.cpp:48: error: expected ';' before 'Test'
moc_q_invokable.cpp:54: error: 'Test' has not been declared
make: *** [moc_q_invokable.o] Error 1
如果去掉命名空间Type或者在moc_q_invokable.cpp里面加上using namespace Type;,编译都可以顺利通过。
有人遇到过这个问题吗?有没有更好的解决办法?