继承至QObject的类使用QMetaObject不能找到相应方法(Q_INVOKABLE)
来源:互联网 发布:mac怎么设置鼠标滚轮 编辑:程序博客网 时间:2024/06/14 17:28
I can not get methods from QMetaObject, methods offset and count are equals
i have a class that inherits from QObject and have the Q_OBJECT macro:
class SomeClass: public QObject{ Q_OBJECTpublic: SomeClass(QObject *parent = 0); void method1(); void method2(); ...};
in another class in the same header i create an instance of that class, and then i try to get all Methods from 'SomeClass' and store it in a QMap:
this->someclass = new SomeClass(); // in constructor.
...
cout<<"init some class methods"<<endl; const QMetaObject *metaobj = dynamic_cast<QObject*>(this->someclass)->metaObject(); cout<<"offset "<<metaobj->methodOffset()<<endl; for(int i = metaobj->methodOffset();i < metaobj->methodCount();i++){ QMetaMethod metamethod = metaobj->method(i); //if(metamethod.methodType() == QMetaMethod::Method){ QString methodname = QString(metamethod.signature()); methodname = methodname.replace(QRegExp("\\(.*\\)"),""); controlmethods.insert(methodname,metamethod); cout<<"added method: "<<metamethod.signature()<<" as "<<methodname.toAscii().data()<<endl; //} }
But this do not showed me any method added because the methods offset is equals to the methods count, why can be? i dont get with the reason, Thanks any help.
2 Answers 2
You need to use the Q_INVOKABLE
macro for each method you want to see in theQMetaObject
.
From the documentation:
Q_INVOKABLE
Apply this macro to definitions of member functions to allow them to be invoked via the meta-object system. The macro is written before the return type, as shown in the following example:
class Window : public QWidget { Q_OBJECTpublic: Window(); void normalMethod(); Q_INVOKABLE void invokableMethod(); };
The invokableMethod() function is marked up using Q_INVOKABLE, causing it to be registered with the meta-object system and enabling it to be invoked using QMetaObject::invokeMethod(). Since normalMethod() function is not registered in this way, it cannot be invoked using QMetaObject::invokeMethod().
You can also use the slots
macro. I think Q_INVOKABLE
may be more minimal, though.
QMetaObject is only aware of signals, slots, properties and other invokable member functions, occasionally referred to as "meta-methods" as a group.
Also, for the first line of your example, you should (probably) just call
const QMetaObject *metaobj = someClass->metaObject();
This isn't just cosmetic. The dynamic_cast
would move type-checking to runtime, which isn't necessary if you know at compile time thatsomeClass
is a pointer to a QObject
-derived class. (dynamic_cast
ing toQObject*
will work, and will get you the correct QMetaObject because of virtual inheritance, but it's unnecessary, less safe, and unclear.)
You don't actually need an instance of the class to get the meta-object:
const QMetaObject *metaobj = SomeClass::staticMetaObject();
This is possible because there is one QMetaObject per class, not per object.
For anyone who wants to know more about the meta-object system, I recommend coffee and thedocumentation. Usually you don't need to deal with QMetaObject instances directly, unless you're writing a scripting engine or something equally 'meta'. It's easy to unintentionally duplicate functionality Qt already provides.
Also, Q_DECLARE_METATYPE
is not what you want.
There is some kind of ambiguity in the official docs.
First we see:
method() and methodCount() provide information about a class's meta-methods (signals, slots and otherinvokable member functions).
But later:
int QMetaObject::methodCount() const Returns the number of methods in this class, including the number of properties provided by each base class. These include signals and slots as well asnormal member functions.
But actually we can't have access to 'normal' methods through Qt MetaObject System.
So, to get access to your methods, you should declare them with the Q_INVOKABLE
macro:
Q_INVOKABLE void method1();Q_INVOKABLE void method2();
methodCount()
, but 4.8 is different, it says directly: "member functions declared with the Q_INVOKABLE macro". – hank Jan 29 '13 at 7:31- 继承至QObject的类使用QMetaObject不能找到相应方法(Q_INVOKABLE)
- QMetaObject::invokeMethod and Q_INVOKABLE
- Qt函数connectSlotsByName(),即"QMetaObject::connectSlotsByName(QObject *o)"的说明
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码 介绍 connectSlotsByName 是一个QMetaObject类里的
- QML用Q_INVOKABLE调用C++的方法
- 使用websphere反复出现不能找到spring方法的错误
- 使用Qt的QObject类实现多叉树
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- Qt 静态函数QMetaObject::connectSlotsByName(QObject * object)
- QMetaObject使用
- Q_INVOKABLE
- Qt使用多线程的一些心得——2.继承QObject的多线程使用方法
- 三、qml调用Q_INVOKABLE方法
- iOS MPMoviePlayerController使用,播放视频
- iOS UITabBarController
- php做推送服务端实现ios消息推送
- 2015-01-23 记录我喜欢的文
- 技术需要耐心
- 继承至QObject的类使用QMetaObject不能找到相应方法(Q_INVOKABLE)
- ITOO---第一阶段小结
- 进程的基本操作
- c语言中变量的存储区域和时效
- Android视图状态及重绘流程分析,带你一步步深入了解View(二)
- linux中errno使用
- 百度和谷歌殊途同归
- Python3.x和Python2.x的区别
- [IOS] view的frame和bounds之区别(位置和大小)