3

Invoking a method

Normal way :

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

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

QMetaObject::invokeMethod(obj, function());
// or any macro like SLOT
QMetaObject::invokeMethod(obj, FUNC_NAME(function()));
JustWe
  • 4,250
  • 3
  • 39
  • 90

1 Answers1

0

I strongly recommend you to use the normal way, i.e. using QMetaObject::invokeMethod(obj, "function"). However, if you want you can use the following stringify macro:

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

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

The above macro convert argument to string then remove method/function arguments between (...).

putu
  • 6,218
  • 1
  • 21
  • 30
  • The stringify macro provides no compile-time protection against a change in the function signature, unlike the Qt 5.10 function pointer syntax overloads which result in compiler errors if the function name does not exist. – pixelgrease Nov 03 '18 at 20:19