简介
无聊使用了一下go-echarts, 使用Qt在C/S模式下嵌入使用B/S框架的简单例子
材料
Qt 5.15.0 + MSVC-2019-64bit
Golang1.14.3 + go-echarts
代码
Golang (Server端)
浏览器 localhost:8081 可以进行访问, 示例来自于 https://go-echarts.github.io/go-echarts/docs/kline
package main
import ("github.com/go-echarts/go-echarts/charts""log""math/rand""net/http""os""time"
)var nameItems = []string{"衬衫", "牛仔裤", "运动裤", "袜子", "冲锋衣", "羊毛衫"}
var foodItems = []string{"面包", "牛奶", "奶茶", "棒棒糖", "加多宝", "可口可乐"}var seed = rand.NewSource(time.Now().UnixNano())
func randInt() []int {cnt := len(nameItems)r := make([]int, 0)for i := 0; i < cnt; i++ {r = append(r, int(seed.Int63())%50)}return r
}// 或者使用 net/http,同上,后面也不就列出
func handler(w http.ResponseWriter, _ *http.Request) {bar := charts.NewBar()bar.SetGlobalOptions(charts.TitleOpts{Title: "Bar-示例图"}, charts.ToolboxOpts{Show: true})bar.SetSeriesOptions(charts.LabelTextOpts{Show: false})bar.AddXAxis(nameItems).AddYAxis("商家A", randInt()).AddYAxis("商家B", randInt())f, err := os.Create("bar.html")if err != nil {log.Println(err)}bar.Render(w, f) // Render 可接收多个 io.writer 接口
}func main() {http.HandleFunc("/", handler)http.ListenAndServe(":8081", nil)
}
Qt (Browser端)
.pro
QT += webenginewidgets
mainwindow.ui
拖一个LineEdit 做网址输入,Pushbutton刷新(或回车自动刷新), widget 提升为QWebEngineView
mainwindow.cpp
// 在(按键)刷新槽函数内加入下面三句
QUrl url = QUrl::fromUserInput(ui->edtUrl->text());
ui->webView->load(url);
ui->webView->setFocus();
效果展示
参考
go-echarts - https://go-echarts.github.io/go-echarts/docs/kline