利用golang和QML來開發Ubuntu應用

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

在前面的文章“使用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


首先就像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/bashDIR=$(dirname $(readlink -f "$0"))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 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:armhfGO_DIR=$DIR/../go-installationmkdir -p $GO_DIRcd $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.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"    /*     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 mainimport (        "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.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)        }()}

具體關於這些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

所有的源碼包括下載的庫等: git clone https://gitcafe.com/ubuntu/ubuntu-go-qml-template.git


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.