![](/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.