Megidd
Megidd

Reputation: 7961

Error: Unknown method parameter type: QString&

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?

Upvotes: 2

Views: 2338

Answers (1)

eyllanesc
eyllanesc

Reputation: 244132

The QML engine converts the appropriate data types by copying the values.

In your case QString & is a reference to a QString that can not be copied, instead const QString & can be copied. For this reason you can not have a QObject as an argument since it is not copyable and instead you must use QObject * since a pointer is copyable.

It's the same principle as the Q_SIGNALs.

Upvotes: 4

Related Questions