This is a creation in Article, where the information may have evolved or changed.
In the previous article, "Using Golang to design our Ubuntu scope," We've covered how to use Golang to develop Ubuntu scope. In today's article, let's take a brief look at how to use Golang to develop QML applications. This is a good choice for some of the familiar Golang languages, but not very familiar with C + + development. While most of our QML applications require only QML with some JavaScript scripts, we can use QT C + + or go language to expand its functionality to do some of the things that require calculations or special features.
First, let's look at a repository made by our Chinese developer Dawndiy:
Https://github.com/dawndiy/ubuntu-go-qml-template
This repository is based on another repository:https://github.com/go-qml/qml
First, as Dawndiy describes in its github:
Installing the Ubuntu SDK
We follow the link "http://developer.ubuntu.com/start/ubuntu-sdk/installing-the-sdk/" To install our own SDK. We can also refer to my blog post "Ubuntu SDK installation".
Install additional Packages
$sudo apt-get Install Golang g++ qtdeclarative5-dev qtbase5-private-dev qtdeclarative5-private-dev Libqt5opengl5-dev Qtdeclarative5-qtquick2-plugin
These packages are necessary for us to successfully compile our GO+QML application.
Set Chroots
If you have already installed your SDK with the Ubuntu SDK installation above, many parts of this section have been done. We execute the following directives:
$git Clone HTTPS://GITHUB.COM/NIKWEN/UBUNTU-GO-QML-TEMPLATE.GIT$CD ubuntu-go-qml-template$chroot-scripts/ setup-chroot.sh
We can do the above things in our own favorite catalogue. In the actual operation, I found that if there is no VPN, the installation may not succeed because it needs to visit the "storage.googleapis.com" url to download something. But it doesn't matter, you can go to Dawndiy's github to download. There's already all the things you need.
Let's take a look at setup-chroot.sh:
#!/bin/bashdir=$ (dirname $ (readlink-f "$)") echo "=====================================" echo "========== Creating Chroot ========== "echo" ===================================== "Echosudo click Chroot-a armhf-f ubuntu-sdk-14.10-s Utopic Createsudo Click chroot-a armhf-f ubuntu-sdk-14.10-s utopic Upgradeechoecho "================================== = = = "echo" = = = Installing packages in chroot = = "echo" ===================================== "Echosudo click Chroot-a armh F-f ubuntu-sdk-14.10-s utopic maint apt-get install git qtdeclarative5-dev:armhf qtbase5-private-dev:armhf qtdeclarativ E5-PRIVATE-DEV:ARMHF LIBQT5OPENGL5-DEV:ARMHF qtdeclarative5-qtquick2-plugin:armhfgo_dir= $DIR/.. /go-installationmkdir-p $GO _dircd $GO _dir$dir/install-go-1-3-3.sh
Here we can see it to download Ubuntu-sdk-14.10 's ARMHF. This is to cross-assemble our application and compile the arm version of the executable file. Of course we can set it to ubuntu-sdk-15.04. We need to make some changes. Once you've set up this step, you don't have to do it a second time.
Run the app under desktop
$./run.sh
As we type the above command in terminal, we can see the following running applications in desktop:
The QML files in your app can be found in the./SHARE/UBUNTU-GO-QML-TEMPLATE/MAIN.QML:
Main.qml
Import QtQuick 2.0import 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" /* The 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 (height:units.gu) page {title:i18n.tr ("simple") Column {spacing:units.gu (1) Anchors {margins:units.gu (2) Fill: Parent} label {Id:label objectName: ' label ' TEXT:CT Rl.message} Button { ObjectName: "button" Width:parent.width text:i18n.tr ("Tap me!") OnClicked:ctrl.hello ()}}}}
In./src/ubuntu-go-qml-template/main.go, we can see the following code:
Package Mainimport ("gopkg.in/qml.v1" "Log") func main () {err: = qml. 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. ObjectMessage 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)} ()}
Specific about these Golang introduction, we can refer to http://godoc.org/gopkg.in/qml.v1.
Deploy to Phone
$./install-on-device.sh
We use the above command to deploy our Go app to the phone:
We can see that it runs exactly the same as it does on the desktop. We can find the click installation files we need in the following directory:
Bin/ubuntu-go-qml-template.nikwen_0.1_armhf.click
All sources include downloaded libraries, etc.: Git clone https://gitcafe.com/ubuntu/ubuntu-go-qml-template.git
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.