Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QMetaObject::indexOfMethod always returns -1

[Solved] QMetaObject::indexOfMethod always returns -1

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mandaarp
    wrote on last edited by
    #1

    I have simple class implementation:

    #ifndef SAMPLE_H
    #define SAMPLE_H

    #include <QObject>

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

    signals:

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

    #endif // SAMPLE_H

    #include "sample.h"

    Sample::Sample(QObject *parent) :
    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();
    sampleSlot(a);
    }

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mandaarp
      wrote on last edited by
      #2

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

      Verifying...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mandaarp
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved