아래와 같이 표준 QT 예제 목록의 약간 수정 된 birthday_party.h
example을 고려하십시오.QML을 실행하는 중에 '알 수없는 메소드 매개 변수 유형'오류가 발생했습니다.
아래 코드에서는 전달 된 포인터를 사용하는 사람의 이름을 출력하는 스텁 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()
에 대한 정의 내가 테스트
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])
}
}
이제 위의 코드를 컴파일하고 잘 실행입니다 전화
void BirthdayParty :: test(Person* p)
{
QString qname = p->name();
std::cout << qname.toUtf8().constData() << std::endl;
}
내 QML 파일입니다. 그러나 test()
의 인수 목록에 Person* p
앞에 const
한정자를 추가하면 QML에서 런타임에 오류가 발생합니다! 내가 런타임에 얻을 오류가
qrc:example.qml:17: Error: Unknown method parameter type: const Person*
입니다
그것은 내 오류 버그에 here를보고 한 것과 동일 보인다 (두 헤더 테스트 및 .CPP에 대한 서명이 void test(const Person* p)
경우 즉, 런타임 barfs) -reports 웹 사이트. Qt의 최신 버전 인 Qt 5.10을 사용하고 있습니다. Person
및 BirthdayParty
유형으로 등록되고
편집 내가 이해 Qt는의 마지막 버전이 그 반대의 경우도 마찬가지 QT하고 QML에서 캐스팅 객체를 변경했다,
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
qmlRegisterType<Person>("People", 1,0, "Person");
항목을 어떻게 등록합니까? – folibis
@folibis 편집을 참조하십시오. 위의'const' 한정자를 추가하여 실험하면서 유형을 등록하는 방법을 변경하지 않았습니다. 그게 효과가 있니? – smilingbuddha