C++ 访问派生类的QMetaObject
我有一个继承的C++ 访问派生类的QMetaObject,c++,qt,qt5,C++,Qt,Qt5,我有一个继承的基类。它应该检查从它派生的类的可调用方法。我想知道我是否可以在某个时候访问派生类的。这里有一个最小的例子 // class definition of base class BaseClass : public QObject { Q_OBJECT public: explicit BaseClass(QObject *parent = nullptr); Q_INVOKABLE int doSomething(const QString& inp
基类。它应该检查从它派生的类的可调用方法。我想知道我是否可以在某个时候访问派生类的。这里有一个最小的例子
// class definition of base
class BaseClass : public QObject
{
Q_OBJECT
public:
explicit BaseClass(QObject *parent = nullptr);
Q_INVOKABLE int doSomething(const QString& input1);
};
// class definition of the derived class
class UserClass : public BaseClass
{
Q_OBJECT
public:
explicit UserClass(QObject* parent = nullptr);
Q_INVOKABLE int doSomethingElse(const QString& input1);
};
具体实施如下:
BaseClass::BaseClass(QObject *parent) : QObject(parent)
{
auto count = this->metaObject()->methodCount();
auto offset = this->metaObject()->methodOffset();
for(int i = offset ; i < count ; ++i)
{
auto method = this->metaObject()->method(i);
qDebug() << method.name() <<method.parameterNames();
}
}
int BaseClass::doSomething(const QString &input1)
{
return input1.toInt();
}
UserClass::UserClass(QObject *parent): BaseClass(parent)
{
}
int UserClass::doSomethingElse(const QString &input1)
{
return input1.length();
}
BaseClass::BaseClass(QObject*parent):QObject(parent)
{
自动计数=此->元对象()->methodCount();
自动偏移=此->元对象()->methodOffset();
对于(int i=偏移量;i<计数;++i)
{
自动方法=此->元对象()->方法(i);
qDebug()好的,我以某种方式让它工作了,但也欢迎使用其他方法。
不可能在基类的构造函数中直接执行,因为派生类在该点上没有实例化。
因此,以下是我的实现:
BaseClass::BaseClass(QObject *parent) : QObject(parent)
{
QTimer::singleShot(0,this,[this](){this->check();});
}
void BaseClass::check()
{
auto count = this->metaObject()->methodCount();
auto offset = this->metaObject()->methodOffset();
for(int i = offset ; i < count ; ++i)
{
auto method = this->metaObject()->method(i);
qDebug() << method.name() <<method.parameterNames();
}
}
BaseClass::BaseClass(QObject*parent):QObject(parent)
{
QTimer::singleShot(0,this,[this](){this->check();});
}
void基类::check()
{
自动计数=此->元对象()->methodCount();
自动偏移=此->元对象()->methodOffset();
对于(int i=偏移量;i<计数;++i)
{
自动方法=此->元对象()->方法(i);
qDebug()