Tôi đang cố gắng cập nhật biểu mẫu WebView
trong QML tuy nhiên tôi đang gặp sự cố khi cập nhật chế độ xem và văn bản bằng GoLang.Các yếu tố cập nhật qt gui không hoạt động với Golang
Tôi đã xem các bài đăng tương tự như this one và this one, nhưng vẫn không rõ ràng.
Như bạn có thể thấy bên dưới, tôi đang cố gắng cập nhật WebView
để thay đổi trang được hiển thị và phần tử Văn bản để tôi có thể xem vì mục đích riêng của mình khi lưu nút. Tuy nhiên GUI không thay đổi.
Những gì tôi đã có cho đến nay là thế này:
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
}
và file 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
}
}
}