简体   繁体   English

运行QML时出现“未知方法参数类型”错误

[英]`Unknown method parameter type` error while running QML

Consider the slightly modified birthday_party.h example from the standard QT example lists below. 考虑稍微修改birthday_party.h 例如从标准QT例如下面列出。

In the code below, I have added a stub test() function which just prints the name of the person using the pointer passed to it. 在下面的代码中,我添加了一个存根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

The definition for test() is test()的定义是

void BirthdayParty :: test(Person* p)
{
  QString qname = p->name();
  std::cout << qname.toUtf8().constData() << std::endl;
}

My QML file where I call test is 我称之为测试的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])
        }
}

Now the above code compiles and runs just fine. 现在,上面的代码可以编译并正常运行。 However, if I add the const qualifier in front of Person* p in the argument list of test() I get errors from QML at run-time! 但是,如果在test()的参数列表中的Person* p前面添加const限定词,则会在运行时从QML中得到错误! (ie runtime barfs if the signature for test in both headers and .cpp is void test(const Person* p) ) (即运行时barfs,如果标头和.cpp中用于测试的签名均为void test(const Person* p)

The error I get at runtime is 我在运行时遇到的错误是

qrc:example.qml:17: Error: Unknown method parameter type: const Person*

It seems my error is identical to the one reported here on the bug-reports website. 看来我的错误是相同的一个报道错误的报告网站上。 I am using Qt 5.10, the latest version of Qt. 我正在使用Qt 5.10,它是Qt的最新版本。

EDIT 编辑

The Person and BirthdayParty types are being registered as follows PersonBirthdayParty类型的注册如下

 qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
 qmlRegisterType<Person>("People", 1,0, "Person");

Ok, as I understand the last versions of Qt had changed object casting from QML to Qt and vice versa. 好的,据我了解,Qt的最新版本已将对象转换从QML更改为Qt,反之亦然。 Sometimes it was able to use pointers to object, references etc. It looks that now it's different and you have to specify the used type explicitly. 有时它能够使用指向对象,引用等的指针。现在看起来已经不一样了,您必须显式指定使用的类型。 In your case I guess you should add the following line to items registration: 对于您的情况,我想您应该在商品注册中添加以下行:

qRegisterMetaType<Person*>("const Person*");

Btw, I advice you to use references instead of pointers since it eliminates the ambiguity. 顺便说一句,我建议您使用引用而不是指针,因为它消除了歧义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM