Extending QML - Methods Example | Qt QML 5.9

Qt 5.9 | Qt QML | Qt QML Examples | Extending QML - Methods Example

Qt 5.9.2 Reference Documentation

# Extending QML - Methods Example

This example builds on:

- Extending QML - Inheritance and Coercion Example
- Extending QML - Object and List Property Types Example
- Extending QML - Adding Types Example

The Methods Example has an additional method in the `BirthdayParty` class: `invite()`. `invite()` is declared with Q_INVOKABLE so that it can be called from QML.

          Q_INVOKABLE void invite(const QString &name);

In `example.qml`, the `invite()` method is called in the Component.onCompleted signal handler:

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

Files:

- referenceexamples/methods/birthdayparty.cpp
- referenceexamples/methods/birthdayparty.h
- referenceexamples/methods/example.qml
- referenceexamples/methods/person.cpp
- referenceexamples/methods/person.h
- referenceexamples/methods/main.cpp
- referenceexamples/methods/methods.pro
- referenceexamples/methods/methods.qrc