Golang+QML写的一个笔记工具
Tofloor
poster avatar
Bluek404
deepin
2014-11-30 08:10
Author
20141130000349.png看见go-qml又更新了不少东西
然后顺便测试一下Deepin的QML控件
花了20分钟写了个小程序
翻了一遍Deepin的控件貌似没有需要用的= =
就只用了DWindow
直接上源码
  1. package main
  2. import (
  3.         "fmt"
  4.         "os"
  5.         "gopkg.in/qml.v1"
  6. )
  7. func main() {
  8.         if err := qml.Run(run); err != nil {
  9.                 fmt.Fprintf(os.Stderr, "error: %v\n", err)
  10.                 os.Exit(1)
  11.         }
  12. }
  13. func run() error {
  14.         engine := qml.NewEngine()
  15.         engine.Context().SetVar("goObject", &GoObject{})
  16.         component, err := engine.LoadFile("test.qml")
  17.         if err != nil {
  18.                 return err
  19.         }
  20.         window := component.CreateWindow(nil)
  21.         window.Show()
  22.         window.Wait()
  23.         return nil
  24. }
  25. type GoObject struct {
  26.         text []string
  27.         Len  int
  28. }
  29. func (g *GoObject) AddText(text string) {
  30.         g.text=append(g.text, text)
  31.         g.Len=len(g.text)
  32.         qml.Changed(g, &g.Len)
  33. }
  34. func (g *GoObject) GetText(index int) string {
  35.         return g.text[index]
  36. }
  37. func (g *GoObject) DelText(index int) {
  38.         g.text=append(g.text[:index],g.text[index+1:]...)
  39.         g.Len=len(g.text)
  40.         qml.Changed(g, &g.Len)
  41. }
Copy the Code
  1. import QtQuick 2.1
  2. import Deepin.Widgets 1.0
  3. DWindow {
  4.     id: window
  5.     color: Qt.rgba(1.0, 0.5, 0, 0.2)
  6.     width: 300
  7.     height: 300
  8.     TextInput {
  9.         id: input
  10.         x: 6
  11.         y: 7
  12.         width: 288
  13.         height: 20
  14.         font.pixelSize: 12
  15.         onAccepted: goObject.addText(text), text=""
  16.     }
  17.     ListView {
  18.         x: 6
  19.         y: 33
  20.         width: 288
  21.         height: 261
  22.         model: goObject.len
  23.         delegate:
  24.             MouseArea {
  25.             width: window.width
  26.             height: 15
  27.                  onClicked: goObject.delText(index)
  28.                 Text {
  29.                     text: goObject.getText(index)
  30.                 }
  31.             }
  32.     }
  33. }
Copy the Code
顺便吐槽一下Deepin的控件好不稳定啊
运行的时候得启动好几遍
因为有机率runtime直接挂掉
以及QML太不灵活了,以后还是乖乖写html吧

下载(请先确认你安装了Deepin的QML控件,Deepin2014默认有):
64位:dtest.zip
-------------------------------
32位你得自己编译,因为go-qml直接使用了CPP文件所以无法交叉编译
--------------------------------
上面的下载我把engine.LoadFile方法换成了LoadString,所以可以编译成单文件
Reply Favorite View the author
All Replies

No replies yet