2013年9月24日星期二

ubuntu use golang, qml with ubuntu sdk development of desktopapplications ( simple example )

 Looking for a very long time go the gui library , tried gtk, qt , it was found ready to try this qml library , try the next good use. # # Preparation ** 1, Go 1.2RC1 ** go version should not be lower than this , I was released the day after the upgrade in 1.2RC found qml, and tested . ** 2, qml ** Project Home https://github.com/niemeyer/qml is still alpha version . Project home there are various platforms installed after installation method passing the qtcreator of ubuntu sdk plugin also gives installed. Then run 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 trying 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, setting compiler environment ! [qml] (http://images.cnblogs.com/cnblogs_com/hangxin1940/508415/o_qml3.png "qml") in the tools -> options in the build & run entries found qt versions, then add the path of 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 , and here I chose qt quick2 ui. He creates three files, one project file , a source file , there is a current user related xml. First, modify the project file , add ubuntu sdk 's import path . Modify `qmlproject` file extension , in the final surface `List of plugin directories passed to QML runtime` comments add a few lines below : / * List of plugin directories passed to QML runtime * / importPaths: [".", " ;/ usr / bin "," / usr/lib/x86_64-linux-gnu/qt5/qml "] then edit ` qml ` suffix UI files : / / here used quick2 and ubuntu sdk module import QtQuick 2.0 import Ubuntu.Components 0.1 import Ubuntu.Layouts 0.1 MainView {id: root objectName: "mainView" applicationName: "LoginWindow" width: units.gu (50) height: units.gu (30) 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: parent.left height: txtName.height anchors.right: parent.right Label {id: lblUsername width: units.gu (7 ) anchors.verticalCenter: txtName.verticalCenter text: "User Name"} TextField {id: txtName anchors.left: lblUsername.right width: parent.width - lblUsername.width - units.gu (4) anchors.leftMargin: units. gu (4) objectName: "txtName" placeholderText: "type your username" / / focus change events onFocusChanged: {if (focus) {/ / when the focus is on the use of js console output , qml it will default transition binding language to standard output console console.log ("qml: txtName focused")}} onTextChanged: {console.log ("qml:" + txtName.text) / / goObject will be injected , it is a go object / / here we must note , go object's properties or methods go level must be exposed / / But being in qml js call when the first letter must be lowercase , try several times to know 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 "} TextField {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 in the qtcreator the build menu, select run, it will use qmlscene to load the ui, for debugging results. In qtcreator the design seems a bit, so I do not recommend this WYSIWYG editing method , which in ubuntu 13.10 version , qt5 may improve after formally introduced . ** 2 , write main.go ** in qml project directory to write main.go package main import ("github.com / niemeyer / qml" "log") / / 's go for injecting qml structure type GoObject struct { } func (g * GoObject) TxtNameChanged (text string) {log.Println ("go:", text)} func main () {/ / initialize qml qml.Init (nil) / / create engine engine: = qml . NewEngine () / / load the qml component, err: = engine.LoadFile ("atomqq.qml") if err! = nil {panic (err)} / / get the context context: = engine.Context () / / will a go injected into qml object context goObject: = GoObject {} context.SetVar ("goObject", & goObject) / / create qml window window: = component.CreateWindow (nil) / / gain root controls root: = window. Root () / / Name property obtained under space / / obj: = root.ObjectByName ("mainPage") / / obj.Set ("title", "xx login window " ) / / display window window.Show ( ) / / get root control of a property width: = root.Int ("width") log.Println (width) / / set the value of an attribute / / this will form a pixel width increases to departure qt carried back to the form / / Since using qml, qt5 also go in on ubuntu is not stable version , there is also a possible bug. / / qml form at initialization time , seemingly did not draw well, you must have to manually redraw once root.Set ("width", width + 1) / / wait for exit window.Wait ()} and then go run main.go! [qml] (http://images.cnblogs.com/cnblogs_com / hangxin1940/508415/o_qml4.png "qml") can be seen qml signal is triggered correctly , the console also has the output of the

没有评论:

发表评论