使用go-qml从QML获取用户输入

huangapple go评论126阅读模式
英文:

Get user input from QML using go-qml

问题

我正在尝试从qml中获取用户输入,使用gopkg.in/qml.v1包。
成功设置了来自Go的值。但是我无法获取更改后的值。
例如,我从Go代码中将_Name_设置到qml中。用户更改文本字段并按下_Button_后,我读取用户输入并返回"Hello, " + ctrl.Name

这是一个示例:

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/qml.v1"
  5. "os"
  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. component, err := engine.LoadFile("main.qml")
  16. if err != nil {
  17. return err
  18. }
  19. context := engine.Context()
  20. context.SetVar("ctrl", &Control{Name: "Enter your name"})
  21. window := component.CreateWindow(nil)
  22. window.Show()
  23. window.Wait()
  24. return nil
  25. }
  26. type Control struct {
  27. Name string
  28. Message string
  29. }
  30. func (ctrl *Control) Hello() {
  31. go func() {
  32. ctrl.Message = "Hello, " + ctrl.Name
  33. qml.Changed(ctrl, &ctrl.Message)
  34. }()
  35. }

main.qml

  1. import QtQuick 2.0
  2. import QtQuick.Controls 1.1
  3. Rectangle {
  4. id: root
  5. color: "#ffffff"
  6. width: 320
  7. height: 320
  8. TextEdit {
  9. id: textEdit1
  10. x: 8
  11. y: 8
  12. width: 304
  13. height: 20
  14. text: ctrl.name
  15. font.pixelSize: 12
  16. }
  17. Button {
  18. id: button1
  19. x: 8
  20. y: 34
  21. width: 304
  22. height: 27
  23. text: qsTr("Button")
  24. onClicked: {
  25. ctrl.hello()
  26. }
  27. }
  28. Text {
  29. id: text1
  30. x: 8
  31. y: 67
  32. width: 304
  33. height: 23
  34. text: ctrl.message
  35. font.pixelSize: 12
  36. }
  37. }

为了使其工作,我必须在_Button_的onClicked()中添加显式赋值,像这样:

  1. Button {
  2. id: button1
  3. x: 8
  4. y: 34
  5. width: 304
  6. height: 27
  7. text: qsTr("Button")
  8. onClicked: {
  9. ctrl.name = textEdit1.text
  10. ctrl.hello()
  11. }
  12. }

我做错了什么?谢谢。

英文:

I attempting to get user input from qml, using gopkg.in/qml.v1 package.
Setting up values from Go is successful. But I can't get back changed values.
For example, I set Name from go code to qml. After user change text field and push Button, I read user input and push back "Hello, " + ctrl.Name

This is example:

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/qml.v1"
  5. "os"
  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. component, err := engine.LoadFile("main.qml")
  16. if err != nil {
  17. return err
  18. }
  19. context := engine.Context()
  20. context.SetVar("ctrl", &Control{Name: "Enter your name"})
  21. window := component.CreateWindow(nil)
  22. window.Show()
  23. window.Wait()
  24. return nil
  25. }
  26. type Control struct {
  27. Name string
  28. Message string
  29. }
  30. func (ctrl *Control) Hello() {
  31. go func() {
  32. ctrl.Message = "Hello, " + ctrl.Name
  33. qml.Changed(ctrl, &ctrl.Message)
  34. }()
  35. }

main.qml

  1. import QtQuick 2.0
  2. import QtQuick.Controls 1.1
  3. Rectangle {
  4. id: root
  5. color: "#ffffff"
  6. width: 320
  7. height: 320
  8. TextEdit {
  9. id: textEdit1
  10. x: 8
  11. y: 8
  12. width: 304
  13. height: 20
  14. text: ctrl.name
  15. font.pixelSize: 12
  16. }
  17. Button {
  18. id: button1
  19. x: 8
  20. y: 34
  21. width: 304
  22. height: 27
  23. text: qsTr("Button")
  24. onClicked: {
  25. ctrl.hello()
  26. }
  27. }
  28. Text {
  29. id: text1
  30. x: 8
  31. y: 67
  32. width: 304
  33. height: 23
  34. text: ctrl.message
  35. font.pixelSize: 12
  36. }
  37. }

To make this work, I have to add explicit assignment in Button onClicked() like this:

  1. Button {
  2. id: button1
  3. x: 8
  4. y: 34
  5. width: 304
  6. height: 27
  7. text: qsTr("Button")
  8. onClicked: {
  9. ctrl.name = textEdit1.text
  10. ctrl.hello()
  11. }
  12. }

What I do wrong? Thank you

答案1

得分: 4

你必须使用绑定来更新ctrl.name,否则它将保持不变:

  1. TextEdit {
  2. id: textEdit1
  3. x: 8
  4. y: 8
  5. width: 304
  6. height: 20
  7. text: ctrl.name
  8. font.pixelSize: 12
  9. }
  10. Binding { target: ctrl; property: "name"; value: textEdit1.text }

参考:http://qt-project.org/doc/qt-5/qml-qtqml-binding.html

英文:

You have to use a binding to update ctrl.name otherwise it stays the same:

  1. TextEdit {
  2. id: textEdit1
  3. x: 8
  4. y: 8
  5. width: 304
  6. height: 20
  7. text: ctrl.name
  8. font.pixelSize: 12
  9. }
  10. Binding { target: ctrl; property: "name"; value: textEdit1.text }

ref: http://qt-project.org/doc/qt-5/qml-qtqml-binding.html

huangapple
  • 本文由 发表于 2014年9月7日 15:10:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/25707951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定