Skip to content

QmlObject Documentation

Radhi Fadlillah edited this page Dec 2, 2019 · 3 revisions

Contents

Overview

QmlObject is used to make a struct accessible from QML code. To use it, you need to embed qamel.QmlObject to your own struct. For example :

type BackEnd struct {
    qamel.QmlObject

    // These are properties of the QML objects
    _ int    `property:"id"`
    _ string `property:"name"`

    // This is the constructor of the BackEnd
    _ func() `constructor:"init"`

    // These are slots for the BackEnd
    _ func() int    `slot:"getRandomInt"`
    _ func() string `slot:"getRandomString"`

    // These are signals for the BackEnd
    _ func(string) `signal:"stringSubmitted"`

    // These are the normal struct fields that won't be accessible from QML code
    location string
    name string
}

Do note all of those properties, constructor, slots and signals are optional. So, as long as you embed the qamel.QmlObject, that struct will be accessible from QML code :

// This is fine
type BackEnd struct {
    qamel.QmlObject
}

Properties

Properties are the data members of the QmlObject. To define a property, you need to create a blank data field with struct tag named property :

type BackEnd struct {
    qamel.QmlObject
    _ int    `property:"id"`
    _ string `property:"name"`
}

Each properties must fulfill following requirements :

  • Must be declared with a blank data field.
  • Has an unexported name, i.e. started with lowercase character. For example, name is allowed while Name is not.
  • Each property must have an unique name.

By defining a property, qamel will autommatically generate its getter and setter. For example, for property name, the getter is name() while the setter is setName(string).

Constructor

Constructor are the code that will be run when the QmlObject is called in the QML code. To define a constructor, you need to create a blank function field with struct tag named constructor, and define the code for that constructor :

type BackEnd struct {
    qamel.QmlObject
    _ func() `constructor:"init"`
}

// This is the definition for constructor `init`
// which will be called when BackEnd is used in QML code
func (b *BackEnd) init() {
    fmt.Println("Hello")
}

A constructor must fulfill following requirements :

  • Must be declared with a blank function field.
  • The code for that constructor must be defined.
  • Each QmlObject can only have at most one constructor.
  • Has an unexported name, i.e. started with lowercase character. For example, init is allowed while Init is not.
  • The function field must not have any parameter or return value.

Slots

Slot in qamel simply means a method of the QmlObject that defined in Go and can be called from QML code. To define a slot, you need to create a blank function field with struct tag named slot, and define the code for that slot :

type BackEnd struct {
    qamel.QmlObject
    _ func() int                  `slot:"getRandomInt"`
    _ func() string               `slot:"getRandomString"`
    _ func(string, string) string `slot:"concatString"`
}

// These are the definition of each slots
func (b *BackEnd) getRandomInt() int {
    return randomInt
}

func (b *BackEnd) getRandomString() string {
    return randomString
}

func (b *BackEnd) concatString(s1, s2 string) string {
    return s1 + s2
}

A slot must fulfill following requirements :

  • Must be declared with a blank function field.
  • The code for that slot must be defined.
  • Has an unexported name, i.e. started with lowercase character. For example, concatString is allowed while ConcatString is not.
  • The function must have at most one return value. However, it can has as many parameters as needed.
  • Each slot must have an unique name.

Signals

Signal in qamel is used to notify when an event happened. To define a slot, you need to create a blank function field with struct tag named signal :

type BackEnd struct {
    qamel.QmlObject
    _ func(string) `signal:"loaded"`
}

Later, in QML code, we can receive the event like this :

BackEnd {
    id: backEnd
    onLoaded: (data) => {
        // do something with data
    }
}

A signal must fulfill following requirements :

  • Must be declared with a blank function field.
  • Has an unexported name, i.e. started with lowercase character. For example, loaded is allowed while Loaded is not.
  • The function must not have any return value. However, it can has as many parameters as needed.
  • Each signal must have an unique name.

For example on how to use signal, check out the example on using goroutine with QML.

Supported Data Types

For QmlObject, qamel only supports following data types :

  • int
  • int32
  • int64
  • float32
  • float64
  • bool
  • string

For more complex type like array, map, or struct, you have to encode it to JSON string which later can be decoded easily in QML or Go.