QmlObject Documentation (original) (raw)

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 :

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 :

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 :

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 :

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 :

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.