Qt官方示例-QML扩展方法

2020-02-24 09:31:27 浏览数 (1)

❝该示例在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")
}
qml

0 人点赞