2014-08-27 11 views
7

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 onethis 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 
     } 

    } 

} 

Trả lời

1

Ví dụ này nên thay đổi các yếu tố văn bản, và thao tác các webview tải một trang mới dựa trên đầu vào của người dùng:

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")) 
} 

Để làm việc này, bạn phải đặt thuộc tính objectName cho thành phần webview:

WebView 
{ 
    objectName: "tralalala" 
    id: frame 
    url: ctrl.message 
    width: 640 
    height: 300 
    smooth: false 
} 

Trong trường hợp mục đích của bạn là để thao tác các nội dung của webview (đối với cách điền vào mẫu tìm kiếm), có nghĩa là không thể thực hiện bằng cách sử dụng các public api. Tuy nhiên, bạn luôn có thể thao tác url để chứa cụm từ tìm kiếm của mình.

Các vấn đề liên quan