Qt GUI元素在使用Golang时无法更新。

huangapple go评论130阅读模式
英文:

qt gui update elements not working with Golang

问题

我正在尝试在QML中使用GoLang更新WebView的表单,但是在更新视图和文本时遇到了问题。

我已经查看了类似的帖子,例如这个这个,但仍然不清楚。

如下所示,我正在尝试更新WebView以更改显示的页面,以及更新文本元素,以便在按下按钮时可以看到存储的内容。然而,GUI没有改变。

我目前的代码如下:

  1. package main
  2. import (
  3. "time"
  4. "math/rand"
  5. "fmt"
  6. "os"
  7. "gopkg.in/qml.v1"
  8. )
  9. type Control struct {
  10. Root qml.Object
  11. Message string
  12. }
  13. func (ctrl *Control) Savetf1contents(text qml.Object) {
  14. fmt.Println("in Savetf1contents():")
  15. fmt.Println("text:", text.String("text"))
  16. }
  17. func (ctrl *Control) Loadtf1contents(text qml.Object) {
  18. fmt.Println("in Loadtf1contents():")
  19. fmt.Println("text:", text.String("text"))
  20. go func() {
  21. ctrl.Message = "loaded from tf1..."
  22. qml.Changed(ctrl, &ctrl.Message)
  23. }()
  24. }
  25. func main() {
  26. if err := qml.Run(run); err != nil {
  27. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  28. os.Exit(1)
  29. }
  30. }
  31. func run() error {
  32. engine := qml.NewEngine()
  33. component, err := engine.LoadFile("helloworld.qml")
  34. if err != nil {
  35. return err
  36. }
  37. ctrl := Control{Message: "http://google.co.uk"}
  38. context := engine.Context()
  39. context.SetVar("ctrl", &ctrl)
  40. window := component.CreateWindow(nil)
  41. ctrl.Root = window.Root()
  42. rand.Seed(time.Now().Unix())
  43. window.Show()
  44. window.Wait()
  45. return nil
  46. }

QML文件如下:

  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.1
  3. import QtQuick.Window 2.0
  4. import QtWebKit 3.0
  5. ApplicationWindow {
  6. title: qsTr("Dashboard")
  7. width: 640
  8. height: 480
  9. menuBar: MenuBar {
  10. Menu {
  11. title: qsTr("File")
  12. MenuItem {
  13. text: qsTr("Exit")
  14. onTriggered: Qt.quit();
  15. }
  16. }
  17. }
  18. Grid {
  19. columns: 3
  20. spacing: 2
  21. Text {
  22. width: 335
  23. text: qsTr(ctrl.message)
  24. }
  25. Rectangle{
  26. width: 200
  27. height: 30
  28. radius: 3
  29. color: "#fff"
  30. TextInput {
  31. id: form
  32. anchors.left: parent.right
  33. anchors.top: parent.top
  34. anchors.leftMargin: -195
  35. anchors.topMargin: 5
  36. text: qsTr("")
  37. focus: true
  38. width: 200
  39. }
  40. }
  41. Button {
  42. text: qsTr("Search User")
  43. onClicked: {
  44. ctrl.savetf1contents(form)
  45. }
  46. }
  47. }
  48. Grid {
  49. columns: 1
  50. spacing: 2
  51. anchors.top: parent.top
  52. anchors.topMargin: 35
  53. id: text
  54. WebView {
  55. id: frame
  56. url: ctrl.message
  57. width: 640
  58. height: 300
  59. smooth: false
  60. }
  61. }
  62. }
英文:

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:

  1. package main
  2. import (
  3. "time"
  4. "math/rand"
  5. "fmt"
  6. "os"
  7. "gopkg.in/qml.v1"
  8. )
  9. type Control struct {
  10. Root qml.Object
  11. Message string
  12. }
  13. func (ctrl *Control) Savetf1contents(text qml.Object) {
  14. fmt.Println("in Savetf1contents():")
  15. fmt.Println("text:", text.String("text"))
  16. }
  17. func (ctrl *Control) Loadtf1contents(text qml.Object) {
  18. fmt.Println("in Loadtf1contents():")
  19. fmt.Println("text:", text.String("text"))
  20. go func() {
  21. ctrl.Message = "loaded from tf1..."
  22. qml.Changed(ctrl, &ctrl.Message)
  23. }()
  24. }
  25. func main() {
  26. if err := qml.Run(run); err != nil {
  27. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  28. os.Exit(1)
  29. }
  30. }
  31. func run() error {
  32. // qml.RegisterTypes("GoExtensions", 1, 0, []qml.TypeSpec{{
  33. // Init: func(r *GoRect, obj qml.Object) { r.Object = obj },
  34. // }})
  35. engine := qml.NewEngine()
  36. component, err := engine.LoadFile("helloworld.qml")
  37. if err != nil {
  38. return err
  39. }
  40. ctrl := Control{Message: "http://google.co.uk"}
  41. context := engine.Context()
  42. context.SetVar("ctrl", &ctrl)
  43. window := component.CreateWindow(nil)
  44. ctrl.Root = window.Root()
  45. rand.Seed(time.Now().Unix())
  46. window.Show()
  47. window.Wait()
  48. return nil
  49. }

and the QML file:

  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.1
  3. import QtQuick.Window 2.0
  4. import QtWebKit 3.0
  5. ApplicationWindow {
  6. //property alias form: ctrl.message
  7. title: qsTr("Dashboard")
  8. width: 640
  9. height: 480
  10. menuBar: MenuBar {
  11. Menu {
  12. title: qsTr("File")
  13. MenuItem {
  14. text: qsTr("Exit")
  15. onTriggered: Qt.quit();
  16. }
  17. }
  18. }
  19. Grid {
  20. columns: 3
  21. spacing: 2
  22. Text {
  23. width: 335
  24. // text: qsTr("Dashboard")
  25. text: qsTr(ctrl.message)
  26. }
  27. Rectangle{
  28. width: 200
  29. height: 30
  30. radius: 3
  31. color: "#fff"
  32. TextInput {
  33. id: form
  34. anchors.left: parent.right
  35. anchors.top: parent.top
  36. anchors.leftMargin: -195
  37. anchors.topMargin: 5
  38. text: qsTr("")
  39. focus: true
  40. width: 200
  41. }
  42. }
  43. Button {
  44. text: qsTr("Search User")
  45. onClicked: {
  46. ctrl.savetf1contents(form)
  47. }
  48. }
  49. }
  50. Grid {
  51. columns: 1
  52. spacing: 2
  53. anchors.top: parent.top
  54. anchors.topMargin: 35
  55. id: text
  56. WebView {
  57. id: frame
  58. url: ctrl.message
  59. width: 640
  60. height: 300
  61. smooth: false
  62. }
  63. }
  64. }

答案1

得分: 1

这个例子应该改变文本元素,并根据用户输入操作webview加载新页面:

  1. func (ctrl *Control) Savetf1contents(text qml.Object) {
  2. fmt.Println("in Savetf1contents():")
  3. fmt.Println("text:", text.String("text"))
  4. // 改变消息并更新
  5. ctrl.Message = text.String("text")
  6. qml.Changed(ctrl, &ctrl.Message)
  7. // 找到webview元素
  8. webframe := ctrl.Root.ObjectByName("tralalala")
  9. // 加载新页面!
  10. webframe.Set("url", text.String("text"))
  11. }

为了使其工作,您必须为webview组件设置objectName属性:

  1. WebView {
  2. objectName: "tralalala"
  3. id: frame
  4. url: ctrl.message
  5. width: 640
  6. height: 300
  7. smooth: false
  8. }

如果您的意图是操作webview的内容(填写搜索表单),那么使用公共API是不可能的。但是,您始终可以通过操作URL来包含您的搜索词。

英文:

This example should change the text element, and manipulate the webview to load new page based on the user input:

  1. func (ctrl *Control) Savetf1contents(text qml.Object)
  2. {
  3. fmt.Println("in Savetf1contents():")
  4. fmt.Println("text:", text.String("text"))
  5. // Change the message & update
  6. ctrl.Message = text.String("text")
  7. qml.Changed(ctrl, &ctrl.Message)
  8. // Find the webview element
  9. webframe := ctrl.Root.ObjectByName("tralalala")
  10. // Load new page!
  11. webframe.Set("url", text.String("text"))
  12. }

In order for this to work, you have to set the objectName property for the webview component:

  1. WebView
  2. {
  3. objectName: "tralalala"
  4. id: frame
  5. url: ctrl.message
  6. width: 640
  7. height: 300
  8. smooth: false
  9. }

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.

huangapple
  • 本文由 发表于 2014年8月27日 19:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/25525931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定