我尝试使用 package.json 从 qml 获取用户输入gopkg.in/qml.v1。从 Go 设置值是成功的。但我无法取回更改后的值。例如,我将名称从 go code设置为 qml。用户更改文本字段并按下Button 后,我读取用户输入并推回"Hello, " + ctrl.Name这是示例:main.gopackage mainimport ( "fmt" "gopkg.in/qml.v1" "os")func main() { if err := qml.Run(run); err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) }}func run() error { engine := qml.NewEngine() component, err := engine.LoadFile("main.qml") if err != nil { return err } context := engine.Context() context.SetVar("ctrl", &Control{Name: "Enter your name"}) window := component.CreateWindow(nil) window.Show() window.Wait() return nil}type Control struct { Name string Message string}func (ctrl *Control) Hello() { go func() { ctrl.Message = "Hello, " + ctrl.Name qml.Changed(ctrl, &ctrl.Message) }()}主文件import QtQuick 2.0import QtQuick.Controls 1.1Rectangle { id: root color: "#ffffff" width: 320 height: 320 TextEdit { id: textEdit1 x: 8 y: 8 width: 304 height: 20 text: ctrl.name font.pixelSize: 12 } Button { id: button1 x: 8 y: 34 width: 304 height: 27 text: qsTr("Button") onClicked: { ctrl.hello() } } Text { id: text1 x: 8 y: 67 width: 304 height: 23 text: ctrl.message font.pixelSize: 12 }}为了完成这项工作,我必须在Button onClicked() 中添加显式赋值,如下所示:Button { id: button1 x: 8 y: 34 width: 304 height: 27 text: qsTr("Button") onClicked: { ctrl.name = textEdit1.text ctrl.hello() }}我做错了什么?谢谢
1 回答
- 1 回答
- 0 关注
- 302 浏览
添加回答
举报
0/150
提交
取消