Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (一) 环境搭建

Note: 开启模块支持, 设置国内高速代理, 参考 - https://www.jianshu.com/p/d782d70b3a25

简介

搭建的目的只是刚好看到有这么一个模块, 还有给使用Go的人需要用到调试界面的时候用一下,真正开发界面并不建议用,没必要, 还有缺点就是在windows平台下编译偏慢, 还有其他一些UI

纯Go开发的UI - https://github.com/mjl-/duit

QtDesigner + Golang 

安装 ui 转 golang 工具 goqtuic

// 执行完毕之后在GOPATH/bin目录下生成 goqtuic.exe 执行程序
go get -u -v github.com/stephenlyu/goqtuic

(1) goqtuic.exe -h
  Usage of E:\CodePath\Golang\bin\goqtuic.exe:
  -go-test-file string  // 生成测试文件
        Test go file path 
  -go-ui-dir string // ui文件转换成的go文件放置路径
        Generated ui go files directory (default "uigen") 
  -ui-file string // ui文件的目录或者路径
        QT Designer ui file or directory (default "ui")

(2) 将xx.ui文件转化成golang文件并放到目录uitogo下
    goqtuic -go-ui-dir="uitogo" -ui-file=xx.ui 

   使用QtDesigner设计视图, 并保存

     (1) QtDesigner编辑如下UI, 并保存为login.ui

     

  (2) 使用 goqtuic 工具将login.ui转成 golang代码

  goqtuic -go-ui-dir="uitogo" -ui-file=login.ui 

此时login.ui所在目录下有了新的目录uitogo, 且uitogo目录下有文件login_ui.go生成 

此时还运行不起来, 因为生成的login_ui.go是基于 github.com/therecipe/qt 的,还需要搭建这个环境

Golang使用Qt库的环境搭建

1. 下载 github.com/therecipe/qt

    备用高速仓库 - https://gitee.com/xuyanhui_halo/qt

    解压

2. mingw64 + qt5.13.0环境包

    https://github.com/therecipe/env_windows_amd64_513

    备用高速仓库 - https://gitee.com/xuyanhui_halo/env_windows_amd64_513

    下载后解压, 修改

    xx\env_windows_amd64_513\5.13.0\mingw73_64\bin\qtenv2.bat 

例如: (注意Path改成你自己编译包放置路径, 注意/\斜杠, 最后bin要/)

@echo off
echo Setting up environment for Qt usage...
set PATH=E:\CodePath\Golang\bin\env_windows_amd64_513\5.13.0\mingw73_64/bin;E:\CodePath\Golang\bin\env_windows_amd64_513\Tools\mingw730_64/bin;%PATH%
echo To export the current PATH to your default CMD or PS env run
echo ------------------------
echo set PATH "%PATH%"
echo ------------------------
echo and re-open the command line window

3. 编译 Qt支持golang工具

    1)  将 2 中mingw bin添加到系统环境中, 为下面编译提供 gcc g++, 关键是后续将go使用Qt库编译时链接的库不会报错

    2) 编译1中解压的源码

$ cd xx\qt\cmd
$ go build ./qtsetup/
$ go build ./qtminimal/
$ go build ./qtdeploy/
$ go build ./qtminimal/
$ go build ./qtrcc/

    编译之后生成, 将他们拷贝放到$GOPATH/bin 目录下即可

      至此环境搭建基本完成

Goland上 配置 ui -> golang 和 编译Qt支持的go源码

ui -> go 外部工具配置

Program: E:\CodePath\Golang\bin\goqtuic.exe

Arguments: -go-ui-dir=$FileDir$ -ui-file=$FilePath$

Working directory: $FileDir$

设置编译带Qt支持的Go文件工具

(1) 重新进行编译

Program: E:\CodePath\Golang\bin\qtdeploy.exe

Argument: -qt_dir=E:\CodePath\Golang\bin\env_windows_amd64_513 -qt_version="5.13.0" build desktop $FileName$

Working directory: $FileDir$

使用

1. 中编译的代码

main.go

package main

import (
	"github.com/therecipe/qt/widgets"
	"github.com/therecipe/qt/core"
	"os"
	myui "go_with_qt/ui"
)

func main() {
	widget := widgets.NewQApplication(len(os.Args), os.Args)

	mainwindows := widgets.NewQMainWindow(nil, core.Qt__Window) // 创建主窗体
	loginUI := myui.UILoginMainWindow{}
	loginUI.SetupUI(mainwindows) // 将UI初始化给创建的mainwindows
	mainwindows.Show() // 显示

	widget.Exec()
}

2. 使用创建的goland工具编译/命令编译

命令: qtdeploy.exe -qt_dir=E:\CodePath\Golang\bin\env_windows_amd64_513 -qt_version=5.13.0 build desktop main.go

3. 编译之后生成windows 和  deploy目录

其中 deploy\windows 目录下有执行文件,双击运行

问题

time="2020-06-18T20:39:37+08:00" level=error msg="failed to run command" _func=RunCmd cmd="go mod vendor" dir="E:\\Code\\Golang\\go_with_qt\\main.go" env= error="fork/exec C:\\Go\\bin\\go.exe: The directory name is invalid." name="go mod vendor"

解决 go mod vendor , 将依赖拷贝到当前目录中, 再重新编译

资源

本例程代码 - https://download.csdn.net/download/halo_hsuh/12533440

                   - 和彩云 http://caiyun.feixin.10086.cn/dl/0r5Csgu39kyu2  提取密码:B3dA

猜你喜欢

转载自blog.csdn.net/halo_hsuh/article/details/106818987