❝该示例在BirthdayParty类中有一个附加方法:invite()。invite()用Q_INVOKABLE声明,以便可以从QML调用它。❞
代码语言:javascript复制class BirthdayParty : public QObject
{
...
Q_INVOKABLE void invite(const QString &name);
...
}
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
在下列QML代码中,该invite()方法在Component.onCompleted信号处理程序中调用:
代码语言:javascript复制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" }
]
Component.onCompleted: invite("William Green")
}