Search code examples
c++qtqtcoreqt-signalsqmetaobject

No matching function to call - compiler says I call (QObject *&) when call is (QObject *)


This is the code:

void invokeQMLFunction2Arg(QObject * object, QString func, QVariant p1, QVariant p2) {
    QMetaObject::invokeMethod(object, func, Qt::DirectConnection, Q_ARG(QVariant, p1), Q_ARG(QVariant, p2));
}

This is the error:

error: no matching function for call to 'QMetaObject::invokeMethod(QObject*&, QString&, Qt::ConnectionType, QArgument<QVariant>, QArgument<QVariant>)'

What am I missing? I call invokeMethod() with a pointer, yet the compiler complains it is a pointer reference.


Solution

  • You are trying to pass a QString to the second argument, whereas that should be const char*. Convert that to the proper type because there is no implicit conversion. You need to decide which way to convert it to QByteArray first, so this is one example that you could potentially write:

    QMetaObject::invokeMethod(object, func.toUtf8().constData(), Qt::DirectConnection, Q_ARG(QVariant, p1), Q_ARG(QVariant, p2));