在前面的文章“使用golang来设计我们的Ubuntu Scope”中,我们已经介绍了如何利用golang来开发Ubuntu Scope。在今天的文章中,我们来简单介绍一下如何使用golang来开发QML应用。这对于一些熟悉golang语言的,但是不是很熟悉C++的开发这来说,无疑是一个好的选择。虽然我们大多数的QML应用只需要QML加上一些Javascript的脚本即可,但是我们可以使用Qt C++或Go语言来拓展它的功能,来做一些需要计算或特殊功能的部分。
首先,我们来查看我们中国开发者dawndiy所做的一个repository:
https://github.com/dawndiy/ubuntu-go-qml-template
这个repository是基于另外一个repository: https://github.com/go-qml/qml.有兴趣的开发者也可以参阅另外一个repository https://github.com/salviati/go-qt5.这也是一个非常有意思的一个项目.
首先就像dawndiy在它的github里描述的那样:
$sudo apt-get install golang g++ qtdeclarative5-dev qtbase5-private-dev qtdeclarative5-private-dev libqt5opengl5-dev qtdeclarative5-qtquick2-plugin
$git clone https://github.com/nikwen/ubuntu-go-qml-template.git
$cd ubuntu-go-qml-template
$chroot-scripts/setup-chroot.sh
#!/bin/bash
DIR=$(dirname $(readlink -f "$0"))
echo "====================================="
echo "========== Creating chroot =========="
echo "====================================="
echo
sudo click chroot -a armhf -f ubuntu-sdk-14.10 -s utopic create
sudo click chroot -a armhf -f ubuntu-sdk-14.10 -s utopic upgrade
echo
echo "====================================="
echo "=== Installing packages in chroot ==="
echo "====================================="
echo
sudo click chroot -a armhf -f ubuntu-sdk-14.10 -s utopic maint apt-get install git qtdeclarative5-dev:armhf qtbase5-private-dev:armhf qtdeclarative5-private-dev:armhf libqt5opengl5-dev:armhf qtdeclarative5-qtquick2-plugin:armhf
GO_DIR=$DIR/../go-installation
mkdir -p $GO_DIR
cd $GO_DIR
$DIR/install-go-1-3-3.sh
$./run.sh
应用中的qml文件可以在./share/ubuntu-go-qml-template/main.qml中找到:
import QtQuick 2.0
import Ubuntu.Components 1.1
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "ubuntu-go-qml-template.nikwen"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(100)
height: units.gu(75)
Page {
title: i18n.tr("Simple")
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Label {
id: label
objectName: "label"
text: ctrl.message
}
Button {
objectName: "button"
width: parent.width
text: i18n.tr("Tap me!")
onClicked: ctrl.hello()
}
}
}
}
package main
import (
"gopkg.in/qml.v1"
"log"
)
func main() {
err := qml.Run(run)
if (err != nil) {
log.Fatal(err)
}
}
func run() error {
engine := qml.NewEngine()
component, err := engine.LoadFile("share/ubuntu-go-qml-template/main.qml")
if err != nil {
return err
}
ctrl := Control{Message: "Hello from Go!"}
context := engine.Context()
context.SetVar("ctrl", &ctrl)
win := component.CreateWindow(nil)
ctrl.Root = win.Root()
win.Show()
win.Wait()
return nil
}
type Control struct {
Root qml.Object
Message string
}
func (ctrl *Control) Hello() {
go func() {
if (ctrl.Message == "Hello from Go!") {
ctrl.Message = "Hello from Go Again!"
} else {
ctrl.Message = "Hello from Go!"
}
qml.Changed(ctrl, &ctrl.Message)
}()
}
$./install-on-device.sh