我正在尝试在QML中更新WebView
表单,但是我在使用GoLang更新视图和文本时遇到问题。
我查看过this one和this one等类似帖子,但目前尚不清楚。
正如您在下面看到的,我正在尝试更新WebView
以更改显示的页面和Text元素,以便我可以看到按下按钮时存储的内容。但是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 {
// 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
}
和QML文件:
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
}
}
}