1

I have one function declared in Foo class:

Q_INVOKABLE void setImageUrl(const QString &imageUrl);

However I cannot get the function index of that method:

Foo* foo = new Foo();
const QMetaObject* metaObject = foo->metaObject();
QString functionNameWithparameter("setImageUrl(QString)");
int functionIndex = metaObject->indexOfMethod(functionNameWithParameter.toStdString().c_str());

if (functionIndex >= 0) {
 // never the case
}

What am I missing?

Niklas
  • 23,674
  • 33
  • 131
  • 170
  • Did you try to do what documentation says, using normalizedSignature()? – Silicomancer Dec 06 '14 at 13:17
  • @Silicomancer: That means signature and not just the name. That is what he is already doing, although wrongly. – László Papp Dec 06 '14 at 13:19
  • `normalizedSignature` did the trick somehow. Do you happen to know the difference between my approach and using `normalizedSignature`? I did debug it and there is literally no difference. – Niklas Dec 06 '14 at 13:22
  • @Niklas: I cannot even reproduce your issue as per my answer. – László Papp Dec 06 '14 at 13:28
  • @Niklas: I simply respected what the documentation says. If there is no difference maybe some side effect solved your problem? Did you do a complete rebuild or something? Does is stop working if you remove normalizedSignature() now? – Silicomancer Dec 06 '14 at 13:34
  • @Silicomancer nope it does not work when I remove `normalizedSignature` – Niklas Dec 06 '14 at 13:39

1 Answers1

0

Apart from the two compiler errors, your approach seems to be correct. I assume that you had some changes that required to rerun moc, but you have not actually done so. This is the working code for me.

main.cpp

#include <QMetaObject>
#include <QDebug>
#include <QString>

class Foo : public QObject
{
    Q_OBJECT
    public:
        explicit Foo(QObject *parent = Q_NULLPTR) : QObject(parent) {}
    Q_INVOKABLE void setImageUrl(const QString &) {}
};

#include "main.moc"

int main()
{
    Foo* foo = new Foo();
    const QMetaObject* metaObject = foo->metaObject();
    QString functionNameWithParameter("setImageUrl(QString)");
    qDebug() << metaObject->indexOfMethod(functionNameWithParameter.toStdString().c_str());
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

5
Niklas
  • 23,674
  • 33
  • 131
  • 170
László Papp
  • 51,870
  • 39
  • 111
  • 135