QMetaObject questions
-
Hi
Say I have a method that takes a point to a base class and I provide this method with a pointer to a class derived from the base.
Now, I want to use QMetaObject to get the class namevoid myMethod(Base* ptr) { QString s = ptr->metaObject()->className(); }
class name is always Base
Is there a wayuof getting the derived class name?
Thanks
-
Hi,
Can you show the declaration of your Base and Derived classes ?
-
Hi,
@GrahamL said:
class name is always Base
Add the
Q_OBJECT
macro to your derived class: http://doc.qt.io/qt-5/qobject.html#Q_OBJECTThen, run qmake again and recompile your program.
-
Hi,
void myMethod(Base* ptr)
{
auto derivedClass = ptr->metaObject()->superClass();
if (derivedClass)
QString s = derivedClass->className();
} -
@Devopia said:
void myMethod(Base* ptr)
{
auto derivedClass = ptr->metaObject()->superClass();
if (derivedClass)
QString s = derivedClass->className();
}superClass()
points to the base class, not the derived class... -
Hi guys
Thanks for your replies
It turns out the the derived class did not have the Q_OBJECT macroAll working now
Thanks
6/6