Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ `未知方法参数类型`运行QML时出错_C++_Qt_Qml - Fatal编程技术网

C++ `未知方法参数类型`运行QML时出错

C++ `未知方法参数类型`运行QML时出错,c++,qt,qml,C++,Qt,Qml,考虑一下下面标准QT示例列表中稍微修改的生日派对.h 在下面的代码中,我添加了一个stubtest()函数,该函数仅使用传递给它的指针打印人员的姓名 #ifndef BIRTHDAYPARTY_H #define BIRTHDAYPARTY_H #include <QObject> #include <QQmlListProperty> #include "person.h" class BirthdayParty : public QObject { Q_

考虑一下下面标准QT示例列表中稍微修改的
生日派对.h

在下面的代码中,我添加了一个stub
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的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()
我在运行时从QML获得错误!(即,如果header和.cpp中的测试签名都是
无效测试(const Person*p)
,则运行时会出现barfs)

我在运行时得到的错误是

qrc:example.qml:17: Error: Unknown method parameter type: const Person*
我的错误似乎与bug报告网站上报告的错误相同。我使用的是Qt5.10,Qt的最新版本

编辑

Person
BirthdayParty
类型的注册如下

 qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
 qmlRegisterType<Person>("People", 1,0, "Person");
qmlRegisterType(“人”,1,0,“生日聚会”);
qmlRegisterType(“人”,1,0,“人”);

好的,据我所知,Qt的最新版本将对象转换从QML更改为Qt,反之亦然。有时它能够使用指向对象、引用等的指针。现在看来它不同了,您必须显式指定使用的类型。 在您的情况下,我想您应该在项目注册中添加以下行:

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

顺便说一句,我建议您使用引用而不是指针,因为这样可以消除歧义。

如何注册您的项目?@folibis请参阅编辑。在尝试添加上面的
const
限定符时,我没有改变注册类型的方式。这有区别吗?
qRegisterMetaType<Person*>("const Person*");