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?
constmeans you might want to be able to change the QString from where the function was called (since you get a reference to the object), however since there is a conversion from javascript to C++ this makes no sense (at least to me ;-) ) - Amfasisitems3D.model[0]? Ifitems3Dis const in that context it will likely beconst QString &, i.e. exactly the type your method needs to accept. You can always addconstto a value's type, i.e. going fromQString &toconst QString &is OK, but you can't take it away (otherwise what's the point, you promise in one place that you won't modify it, but can hand it off to somewhere else that does) - Calethitems3D.model[0]is QML string. QML is like JavaScript, so it might be JavaScript string like"First item". The scenario you suggested makes sense: QML string might be considered unmodifiable (at least when communicating with C++), therefore when passing it to C++, I need to useconstreference! It makes sense :) - user3405291