英文:
Get user input from QML using go-qml
问题
我正在尝试从qml中获取用户输入,使用gopkg.in/qml.v1
包。
成功设置了来自Go的值。但是我无法获取更改后的值。
例如,我从Go代码中将_Name_设置到qml中。用户更改文本字段并按下_Button_后,我读取用户输入并返回"Hello, " + ctrl.Name
这是一个示例:
main.go
package main
import (
"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)
}()
}
main.qml
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
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()
}
}
我做错了什么?谢谢。
英文:
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
package main
import (
"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)
}()
}
main.qml
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
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
}
}
To make this work, I have to add explicit assignment in Button onClicked() like this:
Button {
id: button1
x: 8
y: 34
width: 304
height: 27
text: qsTr("Button")
onClicked: {
ctrl.name = textEdit1.text
ctrl.hello()
}
}
What I do wrong? Thank you
答案1
得分: 4
你必须使用绑定来更新ctrl.name
,否则它将保持不变:
TextEdit {
id: textEdit1
x: 8
y: 8
width: 304
height: 20
text: ctrl.name
font.pixelSize: 12
}
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:
TextEdit {
id: textEdit1
x: 8
y: 8
width: 304
height: 20
text: ctrl.name
font.pixelSize: 12
}
Binding { target: ctrl; property: "name"; value: textEdit1.text }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论