How to call a method by function pointer?

Method call

The usual way:

QMetaObject::invokeMethod(obj, "function");

      

But instead of using string.This what I want is:

QMetaObject::invokeMethod(obj, function());
// or any macro like SLOT
QMetaObject::invokeMethod(obj, FUNC_NAME(function()));

      

+3


source to share


1 answer


I strongly recommend that you use the normal way, i.e. using QMetaObject::invokeMethod(obj, "function")

. However, if you want, you can use the following string macro:

#define FUNC_NAME(a)         (QString(#a).remove(QRegExp("\\((.*)\\)")).trimmed().toLatin1().constData())

//usage
QMetaObject::invokeMethod(obj, FUNC_NAME(function()));

      



The above macro to string conversion argument then removes the method / function arguments in between (...)

.

0


source







All Articles