Using Golang, QML and Ubuntu SDK to develop desktop applications under Ubuntu (simple example)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Looking for a long time go GUI library, try Gtk, ready to try Qt when found this QML library, try the next very useful. # #准备工作 **1, Go 1.2rc1**go version should not be lower than this, I was in 1.2RC released the day after the upgrade found QML, and tested. The **2, qml** Project home page https://github.com/niemeyer/qml is still in the alpha version. The Project home page has the installation method of each platform installed, will incidentally put Qtcreator Ubuntu SDK plugin also installed. Then run the QML sample program github.com/niemeyer/qml/examples/particle! [QML] (http://images.cnblogs.com/cnblogs_com/hangxin1940/508415/o_qml1.png "QML") # #Go QML here try to write a simple login window! [QML] (http://images.cnblogs.com/cnblogs_com/hangxin1940/508415/o_qml2.png "QML") **1, write qml** open Ubuntu SDK Creator, Set the next compilation environment! [QML] (http://images.cnblogs.com/cnblogs_com/hangxin1940/508415/o_qml3.png "QML") build & Run in Tools, options Find QT versions in the entry, and then add the path to Qmake 32-bit:/usr/lib/i686-linux-gnu/qt5/bin/qmake 64-bit:/usr/lib/x86_64-linux-gnu/qt5/ Bin/qmake then create a QML project where you can try to create some sample projects where I chose the QT Quick2 UI. He will create 3 files, a project file, a source file, and an XML related to the current user. First modify the project file, add the Ubuntu SDK import path. Modify the ' qmlproject ' suffix file to add a few lines below the last ' list of plugin directories passed to QML runtime ' Comment:/* List of plugin directories passed to QML Runtime */importpaths: [".", "/usr/bin", "/USR/LIB/X86_64-LINUX-GNU/QT5/QML"] and then edit the ' QML ' suffix of the UI file://Use the qui here Ck2 and Ubuntu SDK module import QtQuick 2.0import ubuntu.components 0.1import ubuntu.layouts 0.1MainView {id:root objectName: "M Ainview "ApplicationName:" Loginwindow "Width:units.gu () Height:units.gu (page {title:" Login Window "ObjectName:" MainPage "Column {anchors.leftMargin:units.gu (2) Anchors.rightMargin:units.gu (2) anchors.topMargin:units.gu (2) Anchors.bottomMargin:units.gu (2) anchors.fill:parent Spacing:units.gu (3) width:parent.width Item {Anchors.left:pare Nt.left height:txtName.height anchors.right:parent.right Label {id:lblusername width:units.gu (7) Anchors.verticalcen Ter:txtName.verticalCenter text: "User Name"} TextField {id:txtname anchors.left:lblUsername.right Width:parent.widt H-lblusername.width-units.gu (4) Anchors.leftMargin:units.gu (4) ObjectName: "Txtname" Placeholdertext: "Type your user Name "//Focus Change Event onfocuschanged: {if (Focus) {//When the focus is given with the JS console output, the QML will default to the console standard output Console.log ("Qml:txtname focused")}} OnTextChanged of the binding language: {Console.log (" QML: "+ txtname.text")//goobject will be injected, it is a Go object//It is important to note that the Go object's properties or methods must be exposed on the go level//But the first letter must be lowercase in the qml when JS is called, and you will know it several times. Goobject.txtnamechanged (txtName.Text)}}} Item {Anchors.left:parent.left height:txtName.height anchors.right:parent . Right Label {id:lblpasswd Width:units.gu (7) anchors.verticalCenter:txtPasswd.verticalCenter text: "Password"} Textfi eld {id:txtpasswd anchors.left:lblPasswd.right width:parent.width-lblpasswd.width-units.gu (4) Anchors.leftmargin: Units.gu (4) ObjectName: "Txtpassword" EchoMode:TextInput.Password text: "Password"}}}} and then select Run from the Qtcreator build menu. It uses Qmlscene to load the UI to debug the effect. Design seems to be a bit problematic in qtcreator, so this WYSIWYG editing method is not recommended, which may be improved after the QT5 is formally introduced in Ubuntu version 13.10. **2, write main.go** in QML project directory to write Main.gopackage mainimport ("github.com/niemeyer/qml" "Log")//GO structure for injection QML type Goobject struct {}func (g *goobject) txtnamechanged (text string) {log. Println ("Go:", tEXT)}func Main () {//Initialize QMLQML. Init (nil)//create Engine: = QML. Newengine ()//Load qmlcomponent, err: = engine. LoadFile ("ATOMQQ.QML") if err! = Nil {panic (err)}//Get context: = engine. Context ()//inject a Go object into the QML context goobject: = Goobject{}context. SetVar ("Goobject", &goobject)//Create QML window windows: = Component. CreateWindow (nil)//Get root control root: = window. Root ()//Get space//obj based on the Name property: = root. Objectbyname ("MainPage")//obj. Set ("title", "XX login Window")//Display Windows window. Show ()//Get a property of the root control width: = root. Int ("width") log. Println (width)///Set the value of a property//This will increase the width of the form by one pixel, to depart Qt to re-return the form//due to the use of QML, QT5 and go in Ubuntu is not stable version, there may be some bug.// QML when the form is initialized, it does not seem to be drawing well, and it must be manually redrawn once root. Set ("width", Width + 1)//Waits to exit window. Wait ()} then go run main.go! [QML] (http://images.cnblogs.com/cnblogs_com/hangxin1940/508415/o_qml4.png "QML") can see the QML signal is correctly triggered, the console also has output
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.