![](/img/trans.png)
[英]QtQuick - qml:28: Error: Unknown method return type: custom type
[英]`Unknown method parameter type` error while running QML
考慮稍微修改birthday_party.h
例如從標准QT例如下面列出。
在下面的代碼中,我添加了一個存根test()
函數,該函數僅使用傳遞給它的指針來打印人的姓名。
#ifndef BIRTHDAYPARTY_H
#define BIRTHDAYPARTY_H
#include <QObject>
#include <QQmlListProperty>
#include "person.h"
class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(Person *host READ host WRITE setHost)
Q_PROPERTY(QQmlListProperty<Person> guests READ guests)
public:
BirthdayParty(QObject *parent = 0);
Person *host() const;
void setHost(Person *);
QQmlListProperty<Person> guests();
int guestCount() const;
Person *guest(int) const;
Q_INVOKABLE Person* invite(const QString &name);
Q_INVOKABLE void test( Person* p);
private:
Person *m_host;
QList<Person *> m_guests;
};
#endif // BIRTHDAYPARTY_H
test()
的定義是
void BirthdayParty :: test(Person* p)
{
QString qname = p->name();
std::cout << qname.toUtf8().constData() << std::endl;
}
我稱之為測試的QML文件是
import QtQuick 2.0
import People 1.0
BirthdayParty {
host: Person { name: "Bob Jones" ; shoeSize: 12 }
guests: [
Person { name: "Leo Hodges" },
Person { name: "Jack Smith" },
Person { name: "Anne Brown" },
Person { name : "Gaurish Telang"}
]
Component.onCompleted:
{
test(guests[0])
}
}
現在,上面的代碼可以編譯並正常運行。 但是,如果在test()
的參數列表中的Person* p
前面添加const
限定詞,則會在運行時從QML中得到錯誤! (即運行時barfs,如果標頭和.cpp中用於測試的簽名均為void test(const Person* p)
)
我在運行時遇到的錯誤是
qrc:example.qml:17: Error: Unknown method parameter type: const Person*
看來我的錯誤是相同的一個報道此錯誤的報告網站上。 我正在使用Qt 5.10,它是Qt的最新版本。
編輯
Person
和BirthdayParty
類型的注冊如下
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
qmlRegisterType<Person>("People", 1,0, "Person");
好的,據我了解,Qt的最新版本已將對象轉換從QML更改為Qt,反之亦然。 有時它能夠使用指向對象,引用等的指針。現在看起來已經不一樣了,您必須顯式指定使用的類型。 對於您的情況,我想您應該在商品注冊中添加以下行:
qRegisterMetaType<Person*>("const Person*");
順便說一句,我建議您使用引用而不是指針,因為它消除了歧義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.