Qt官方示例-QML扩展方法

  • 2020 年 2 月 24 日
  • 筆記

❝该示例在BirthdayParty类中有一个附加方法:invite()。invite()用Q_INVOKABLE声明,以便可以从QML调用它。❞

class BirthdayParty : public QObject  {      ...      Q_INVOKABLE void invite(const QString &name);      ...  }    qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");  

  在下列QML代码中,该invite()方法在Component.onCompleted信号处理程序中调用:

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")  }