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
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With