On C++ side, I had this method:
class MyClass : public QObject
{
Q_OBJECT
Q_INVOKABLE QVariant getFamily_byParentName(QString &parentName) const;
// ...
}
I was calling the C++ method on QML:
onButtonClicked: {
myClass.getFamily_byParentName(items3D.model[0]) // items3D.model[0] is a string
}
The above code was throwing this error at the QML line myClass.getFamily_byParentName(items3D.model[0])
:
Error: Unknown method parameter type: QString&
Solution
The above error got resolved by declaring QString
argument as const
:
Q_INVOKABLE QVariant getFamily_byParentName(const QString &parentName) const;
The question is: why?
The QML engine converts the appropriate data types by copying the values.
In your case
QString &
is a reference to aQString
that can not be copied, insteadconst QString &
can be copied. For this reason you can not have aQObject
as an argument since it is not copyable and instead you must useQObject *
since a pointer is copyable.It's the same principle as the Q_SIGNALs.