在前面的文章“使用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里描述的那样:
安装Ubuntu SDK
我们按照连接“
http://developer.ubuntu.com/start/ubuntu-sdk/installing-the-sdk/”来安装我们自己的SDK。我们也可以参照我的博客文章“
Ubuntu SDK 安装”。
安装额外的包
$sudo apt-get install golang g++ qtdeclarative5-dev qtbase5-private-dev qtdeclarative5-private-dev libqt5opengl5-dev qtdeclarative5-qtquick2-plugin
这些包是为了我们能够成功编译我们的Go+QML应用所必须的。
设置chroots
如果你已经在上面参照“
Ubuntu SDK 安装”来安装自己的SDK的话,这部分的很多部分已经做了。我们执行如下的指令:
$git clone https://github.com/nikwen/ubuntu-go-qml-template.git
$cd ubuntu-go-qml-template
$chroot-scripts/setup-chroot.sh
我们可以在我们自己喜欢的目录中做上面的事情。在实际的操作中,我发现如果在没有VPN的情况下,安装可能不能成功,原因是它需要访问“storage.googleapis.com”网址来下载一些东西。不过这没关系,你们可以到dawndiy的github里下载。那里已经有你所需要的所有的东西。
我们来看一下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
在这里,我们可以看到它去下载ubuntu-sdk-14.10 的armhf。这个是为了来交叉汇编我们的应用,并编译ARM版本的可执行文件。当然我们可以设置为ubuntu-sdk-15.04。我们需要做一些改变。这个步骤一旦设置好了,就可以不用再做第二次了。
在Desktop下运行应用
$./run.sh
我们在Terminal中键入上面的命令,我们就可以在Desktop中看见如下的运行的应用:
应用中的qml文件可以在./share/ubuntu-go-qml-template/main.qml中找到:
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()
}
}
}
}
在./src/ubuntu-go-qml-template/main.go,我们可以看到如下的代码:
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)
}()
}
具体关于这些golang的介绍,大家可以参考 http://godoc.org/gopkg.in/qml.v1。
部署到手机中
$./install-on-device.sh
我们使用上面的命令来部署我们的Go应用到手机中:
我们可以看到它的运行和在Desktop上面的是完全一样的。我们可以在如下的目录中找到我们所需要的click安装文件:
bin/ubuntu-go-qml-template.nikwen_0.1_armhf.click
所有的源码包括下载的库等:
https://github.com/liu-xiao-guo/ubuntu-go-qml-template