英文:
qt gui update elements not working with Golang
问题
我正在尝试在QML中使用GoLang更新WebView
的表单,但是在更新视图和文本时遇到了问题。
如下所示,我正在尝试更新WebView
以更改显示的页面,以及更新文本元素,以便在按下按钮时可以看到存储的内容。然而,GUI没有改变。
我目前的代码如下:
package main
import (
"time"
"math/rand"
"fmt"
"os"
"gopkg.in/qml.v1"
)
type Control struct {
Root qml.Object
Message string
}
func (ctrl *Control) Savetf1contents(text qml.Object) {
fmt.Println("in Savetf1contents():")
fmt.Println("text:", text.String("text"))
}
func (ctrl *Control) Loadtf1contents(text qml.Object) {
fmt.Println("in Loadtf1contents():")
fmt.Println("text:", text.String("text"))
go func() {
ctrl.Message = "loaded from tf1..."
qml.Changed(ctrl, &ctrl.Message)
}()
}
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("helloworld.qml")
if err != nil {
return err
}
ctrl := Control{Message: "http://google.co.uk"}
context := engine.Context()
context.SetVar("ctrl", &ctrl)
window := component.CreateWindow(nil)
ctrl.Root = window.Root()
rand.Seed(time.Now().Unix())
window.Show()
window.Wait()
return nil
}
QML文件如下:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
import QtWebKit 3.0
ApplicationWindow {
title: qsTr("Dashboard")
width: 640
height: 480
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Grid {
columns: 3
spacing: 2
Text {
width: 335
text: qsTr(ctrl.message)
}
Rectangle{
width: 200
height: 30
radius: 3
color: "#fff"
TextInput {
id: form
anchors.left: parent.right
anchors.top: parent.top
anchors.leftMargin: -195
anchors.topMargin: 5
text: qsTr("")
focus: true
width: 200
}
}
Button {
text: qsTr("Search User")
onClicked: {
ctrl.savetf1contents(form)
}
}
}
Grid {
columns: 1
spacing: 2
anchors.top: parent.top
anchors.topMargin: 35
id: text
WebView {
id: frame
url: ctrl.message
width: 640
height: 300
smooth: false
}
}
}
英文:
I'm attempting to make a form update a WebView
in QML however I'm having issues updating the view and text using GoLang.
I've looked at similar posts such as this one and this one, but it is still no clear.
As you can see below, I'm trying to update the WebView
to change the page shown, and the Text element so I can see for my own sake what is being stored when I press the button. However the GUI doesn't change.
What I've got so far is this:
package main
import (
"time"
"math/rand"
"fmt"
"os"
"gopkg.in/qml.v1"
)
type Control struct {
Root qml.Object
Message string
}
func (ctrl *Control) Savetf1contents(text qml.Object) {
fmt.Println("in Savetf1contents():")
fmt.Println("text:", text.String("text"))
}
func (ctrl *Control) Loadtf1contents(text qml.Object) {
fmt.Println("in Loadtf1contents():")
fmt.Println("text:", text.String("text"))
go func() {
ctrl.Message = "loaded from tf1..."
qml.Changed(ctrl, &ctrl.Message)
}()
}
func main() {
if err := qml.Run(run); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
// qml.RegisterTypes("GoExtensions", 1, 0, []qml.TypeSpec{{
// Init: func(r *GoRect, obj qml.Object) { r.Object = obj },
// }})
engine := qml.NewEngine()
component, err := engine.LoadFile("helloworld.qml")
if err != nil {
return err
}
ctrl := Control{Message: "http://google.co.uk"}
context := engine.Context()
context.SetVar("ctrl", &ctrl)
window := component.CreateWindow(nil)
ctrl.Root = window.Root()
rand.Seed(time.Now().Unix())
window.Show()
window.Wait()
return nil
}
and the QML file:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
import QtWebKit 3.0
ApplicationWindow {
//property alias form: ctrl.message
title: qsTr("Dashboard")
width: 640
height: 480
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Grid {
columns: 3
spacing: 2
Text {
width: 335
// text: qsTr("Dashboard")
text: qsTr(ctrl.message)
}
Rectangle{
width: 200
height: 30
radius: 3
color: "#fff"
TextInput {
id: form
anchors.left: parent.right
anchors.top: parent.top
anchors.leftMargin: -195
anchors.topMargin: 5
text: qsTr("")
focus: true
width: 200
}
}
Button {
text: qsTr("Search User")
onClicked: {
ctrl.savetf1contents(form)
}
}
}
Grid {
columns: 1
spacing: 2
anchors.top: parent.top
anchors.topMargin: 35
id: text
WebView {
id: frame
url: ctrl.message
width: 640
height: 300
smooth: false
}
}
}
答案1
得分: 1
这个例子应该改变文本元素,并根据用户输入操作webview加载新页面:
func (ctrl *Control) Savetf1contents(text qml.Object) {
fmt.Println("in Savetf1contents():")
fmt.Println("text:", text.String("text"))
// 改变消息并更新
ctrl.Message = text.String("text")
qml.Changed(ctrl, &ctrl.Message)
// 找到webview元素
webframe := ctrl.Root.ObjectByName("tralalala")
// 加载新页面!
webframe.Set("url", text.String("text"))
}
为了使其工作,您必须为webview组件设置objectName属性:
WebView {
objectName: "tralalala"
id: frame
url: ctrl.message
width: 640
height: 300
smooth: false
}
如果您的意图是操作webview的内容(填写搜索表单),那么使用公共API是不可能的。但是,您始终可以通过操作URL来包含您的搜索词。
英文:
This example should change the text element, and manipulate the webview to load new page based on the user input:
func (ctrl *Control) Savetf1contents(text qml.Object)
{
fmt.Println("in Savetf1contents():")
fmt.Println("text:", text.String("text"))
// Change the message & update
ctrl.Message = text.String("text")
qml.Changed(ctrl, &ctrl.Message)
// Find the webview element
webframe := ctrl.Root.ObjectByName("tralalala")
// Load new page!
webframe.Set("url", text.String("text"))
}
In order for this to work, you have to set the objectName property for the webview component:
WebView
{
objectName: "tralalala"
id: frame
url: ctrl.message
width: 640
height: 300
smooth: false
}
In case your intent was to manipulate the content of webview (for filling out the search form), that is not possible by using the public api. However, you can always manipulate the url to contain your search terms.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论