[Solved] QMetaObject::indexOfMethod always returns -1

  • I have simple class implementation:

    #ifndef SAMPLE_H
    #define SAMPLE_H

    #include <QObject>

    class Sample : public QObject
    explicit Sample(QObject *parent = 0);


    public slots:
    void sampleSlot(int);
    void sampleAnotherSlot(int, int);

    #endif // SAMPLE_H

    #include "sample.h"

    Sample::Sample(QObject *parent) :

    void Sample::sampleSlot(int a)
    a = a + 1;

    void Sample::sampleAnotherSlot(int a, int b)
    a = a+b;
    const QMetaObject* pObject = NULL;
    pObject = metaObject();
    int nCount = pObject->methodCount();

        int nIndex = pObject->indexOfMethod(pObject->normalizedSignature("sampleSlot").data());
        int nSlotIndex = pObject->indexOfSlot("sampleSlot");

    // QMetaMethod objMethod = pObject->method(nIndex);
    // QList<QByteArray> listParams = objMethod.parameterTypes();

    Here, both nIndex and nSlotIndex are -1. I have gone through documentation. sampleSlot and sampleAnotherSlot are public slots. Hence I did not prefix with Q_INVOCABLE.

    I am using Qt 5 for MSVC and MingW.

    Am I missing anything?

  • Ahh.. I think, I need to specify "sampleSlot(int)" instead of "sampleSlot".


  • Got it. Using function signature instead of just function name solved the problem. normalizedSignature literally meant 'function signature' and not just function name. :)

    Including an example for each Qt method in the documentation, would be a great idea.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.