QMetaMethod from member function pointer

Pointing to a pointer to a method of a derived class QObject: is there a way to get the QMetaMethodmethod that the pointer points to? I am basically looking for a type function QMetaMethod::fromSignal, but for slots.


Note: I tried to get the index through static_metacallwith QMetaObject::IndexOfMethodand using this for QMetaObject::method:

void(Class::*method)() = &Class::method;
int methodIndex = -1;
void *metaArgs[] = {&methodIndex, reinterpret_cast<void **>(&method)};
const QMetaObject mo = Class::staticMetaObject;
mo.static_metacall(QMetaObject::IndexOfMethod, 0, metaArgs);
qDebug() << methodIndex;
// QMetaMethod mm = mo.method(methodIndex)

The output is always -1.

+4
source share
1 answer

Right now, the only solution is change moc. The patch is pretty trivial:

diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp
index d831edf..7dcefcc 100644
--- a/src/tools/moc/generator.cpp
+++ b/src/tools/moc/generator.cpp
@@ -1311,15 +1311,12 @@ void Generator::generateStaticMetacall()
             isUsed_a = true;
         }

-    }
-    if (!cdef->signalList.isEmpty()) {
-        Q_ASSERT(needElse); // if there is signal, there was method.
         fprintf(out, " else if (_c == QMetaObject::IndexOfMethod) {\n");
         fprintf(out, "        int *result = reinterpret_cast<int *>(_a[0]);\n");
         fprintf(out, "        void **func = reinterpret_cast<void **>(_a[1]);\n");
         bool anythingUsed = false;
-        for (int methodindex = 0; methodindex < cdef->signalList.size(); ++methodindex) {
-            const FunctionDef &f = cdef->signalList.at(methodindex);
+        for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
+            const FunctionDef &f = methodList.at(methodindex);
             if (f.wasCloned || !f.inPrivateClass.isEmpty() || f.isStatic)
                 continue;
             anythingUsed = true;

Then the following is done:

// https://github.com/KubaO/stackoverflown/tree/master/questions/metamethod-lookup-24577095
#include <QtCore>

class MyObject : public QObject {
   Q_OBJECT
public:
   Q_SLOT void aSlot() {}
   Q_SLOT void aSlot2(int) {}
   Q_SLOT int aSlot3(int) { return 0; }
   Q_SIGNAL void aSignal();
   Q_SIGNAL void aSignal2(int);
};

template <typename Func> int indexOfMethod(Func method)
{
   using FuncType = QtPrivate::FunctionPointer<Func>;
   int methodIndex = -1;
   void *metaArgs[] = {&methodIndex, reinterpret_cast<void **>(&method)};
   auto mo = FuncType::Object::staticMetaObject;
   mo.static_metacall(QMetaObject::IndexOfMethod, 0, metaArgs);
   return methodIndex;
}

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   qDebug() << indexOfMethod(&MyObject::aSlot)
            << indexOfMethod(&MyObject::aSlot3) << indexOfMethod(&MyObject::aSignal2);
   return 0;
}
#include "main.moc"
+3
source

Source: https://habr.com/ru/post/1547188/


All Articles