Error: Unknown method parameter type: QString&

2019-07-28 23:35发布

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?

标签: qt qml
1条回答
SAY GOODBYE
2楼-- · 2019-07-29 00:34

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.

查看更多
登录 后发表回答