LOADING

正在加载

Golang使用GUI图形化QT库

壹 介绍

Qt是一个跨平台的C++开发库,主要用来开发图形用户界面(Graphical User Interface-GUI)程序,当然也可以开发不带界面的命令行(Command User Interface-CUI)程序。Qt是纯C++开发的,Qt还存在PythonRubyPerl等语言的绑定, 也就是说可以使用其他编程语言开发基于Qt的程序。这里我们主要讲解golang安装使用Qt库,golang版本为1.18

贰 安装必要的插件

2.1 QtDesigner可视化工具

QtDesigner可视化工具是Qt程序UI界面的实现工具,使用Qt Designer可以拖拽、点击完成GUI界面设计,并且设计完成的.ui程序可以转换成其他编程语言文件。
该工具的安装的话,如果在Qt官网安装会很大,但是用python去安装会更方便,而且也更小巧。可以参考:QT designer安装及运用

2.2 goqtuic工具

goqtuic工具主要是将ui文件转成golang文件。我们在终端运行下面命令,执行完后会在%GOPATH%/bin目录下生成goqtuic.exe执行程序(需要注意安装该工具需要将GO111MODULE设置为auto并且安装git工具):

go get -u -v github.com/stephenlyu/goqtuic

0ba645cd2ead5da1fd46f094e9955b1b.png
我们在终端运行goqtuic.exe -h出现下面程序说明安装成功:
8a178e5beb6861582ee7f13779edba59.png

2.3 Qt支持Golang的环境

先下载mingw64环境包,链接为传送门, 下载后解压,将其文件夹放到GOPATH下, 修改env_windows_amd64_513\5.13.0\mingw73_64\bin\qtenv2.bat 内容:

@echo off
echo Setting up environment for Qt usage...
set PATH=C:\Users\admin\go\bin\env_windows_amd64\5.13.0\mingw73_64\bin;C:/Users/admin/go/bin/env_windows_amd64_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

说明:
C:\Users\admin\go\bin\env_windows_amd64\5.13.0\mingw73_64\bin;C:/Users/admin/go/bin/env_windows_amd64_Tools/mingw730_64\bin;需要根据实际情况修改。

mingw73_64\bin添加到系统环境中:
aa9dfdf1785d4845346faf4e588b1bc0.png
然后下载golangQt库源码:官网
01f533d89d7918fad0a42ce44ba63642.png
开始编译:

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

719613cf8241e85a730909916ff4f2a0.png
完成前期工作!

叁 使用

首先使用QtDesigner设计运行的图形界面,例如下面这样:
b237bb38b8380778eb0343775d9a1479.png
保存为login.ui,接着使用goqtuic工具将login.ui转成golang代码:

# -go-ui-dir表示将转码好的golang代码放到对应的指定文件夹文件夹下
# -ui-file表示要被转码的ui代码,该ui代码是由QtDesigner可视化工具生成的
goqtuic.exe -go-ui-dir="uifile" -ui-file="login.ui" 

运行完上面命令后,会在ui代码所在目录下有了新的目录uifile,而且uifile目录下有经过转码后的golang文件。
9bd9f726b1040b35038d314c7f2d3776.png
编写golang代码:

  • main.go
    426d29a9849466234db67cf20b2a543f.png
package main

import (
    "demo/uifile"
    "os"

    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
)

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

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

    widget.Exec()
}
  • uifile/login_ui.go
    f845a1c01627a815db1636ebe6843590.png
// WARNING! All changes made in this file will be lost!
package uifile

import (
    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
)

type UILoginMainWindow struct {
    Centralwidget *widgets.QWidget
    VerticalLayout *widgets.QVBoxLayout
    HorizontalLayout *widgets.QHBoxLayout
    Label *widgets.QLabel
    LineEdit *widgets.QLineEdit
    HorizontalLayout2 *widgets.QHBoxLayout
    Label2 *widgets.QLabel
    LineEdit2 *widgets.QLineEdit
    HorizontalLayout3 *widgets.QHBoxLayout
    HorizontalSpacer *widgets.QSpacerItem
    PushButton *widgets.QPushButton
    HorizontalSpacer2 *widgets.QSpacerItem
    Menubar *widgets.QMenuBar
}

func (this *UILoginMainWindow) SetupUI(MainWindow *widgets.QMainWindow) {
    MainWindow.SetObjectName("MainWindow")
    MainWindow.SetGeometry(core.NewQRect4(0, 0, 332, 147))
    this.Centralwidget = widgets.NewQWidget(MainWindow, core.Qt__Widget)
    this.Centralwidget.SetObjectName("Centralwidget")
    this.VerticalLayout = widgets.NewQVBoxLayout2(this.Centralwidget)
    this.VerticalLayout.SetObjectName("verticalLayout")
    this.VerticalLayout.SetContentsMargins(0, 0, 0, 0)
    this.VerticalLayout.SetSpacing(0)
    this.HorizontalLayout = widgets.NewQHBoxLayout2(this.Centralwidget)
    this.HorizontalLayout.SetObjectName("horizontalLayout")
    this.HorizontalLayout.SetContentsMargins(0, 0, 0, 0)
    this.HorizontalLayout.SetSpacing(0)
    this.Label = widgets.NewQLabel(this.Centralwidget, core.Qt__Widget)
    this.Label.SetObjectName("Label")
    this.HorizontalLayout.AddWidget(this.Label, 0, 0)
    this.LineEdit = widgets.NewQLineEdit(this.Centralwidget)
    this.LineEdit.SetObjectName("LineEdit")
    this.HorizontalLayout.AddWidget(this.LineEdit, 0, 0)
    this.VerticalLayout.AddLayout(this.HorizontalLayout, 0)
    this.HorizontalLayout2 = widgets.NewQHBoxLayout2(this.Centralwidget)
    this.HorizontalLayout2.SetObjectName("horizontalLayout_2")
    this.HorizontalLayout2.SetContentsMargins(0, 0, 0, 0)
    this.HorizontalLayout2.SetSpacing(0)
    this.Label2 = widgets.NewQLabel(this.Centralwidget, core.Qt__Widget)
    this.Label2.SetObjectName("Label2")
    this.HorizontalLayout2.AddWidget(this.Label2, 0, 0)
    this.LineEdit2 = widgets.NewQLineEdit(this.Centralwidget)
    this.LineEdit2.SetObjectName("LineEdit2")
    this.HorizontalLayout2.AddWidget(this.LineEdit2, 0, 0)
    this.VerticalLayout.AddLayout(this.HorizontalLayout2, 0)
    this.HorizontalLayout3 = widgets.NewQHBoxLayout2(this.Centralwidget)
    this.HorizontalLayout3.SetObjectName("horizontalLayout_3")
    this.HorizontalLayout3.SetContentsMargins(0, 0, 0, 0)
    this.HorizontalLayout3.SetSpacing(0)
    this.HorizontalSpacer = widgets.NewQSpacerItem(40, 20, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Minimum)
    this.HorizontalLayout3.AddItem(this.HorizontalSpacer)
    this.PushButton = widgets.NewQPushButton(this.Centralwidget)
    this.PushButton.SetObjectName("PushButton")
    this.HorizontalLayout3.AddWidget(this.PushButton, 0, 0)
    this.HorizontalSpacer2 = widgets.NewQSpacerItem(40, 20, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Minimum)
    this.HorizontalLayout3.AddItem(this.HorizontalSpacer2)
    this.VerticalLayout.AddLayout(this.HorizontalLayout3, 0)
    MainWindow.SetCentralWidget(this.Centralwidget)
    this.Menubar = widgets.NewQMenuBar(MainWindow)
    this.Menubar.SetObjectName("Menubar")
    this.Menubar.SetGeometry(core.NewQRect4(0, 0, 332, 23))
    MainWindow.SetMenuBar(this.Menubar)
    this.RetranslateUi(MainWindow)

}

func (this *UILoginMainWindow) RetranslateUi(MainWindow *widgets.QMainWindow) {
    _translate := core.QCoreApplication_Translate
    MainWindow.SetWindowTitle(_translate("MainWindow", "MainWindow", "", -1))
    this.Label.SetText(_translate("MainWindow", "用户名", "", -1))
    this.Label2.SetText(_translate("MainWindow", "密  码", "", -1))
    this.PushButton.SetText(_translate("MainWindow", "登录", "", -1))
}

开始编译:

# mod
go mod init demo
go mod tidy
# 安装依赖
go mod vendor
# 生成GUI界面
qtdeploy.exe -qt_dir="C:\GoPath\bin\env_windows_amd64_513" -qt_version="5.13.0" build desktop .

d5607e95e275c0f804a27241a92a9378.png
如果运行没有报错,会在当前目录下生成windowsdeploy目录,其中deploy\windows目录下有执行文件,就是我们生成的GUI界面,双击运行。
ed02eda8e7940948e018a95d37ba9914.png

肆 参考

avatar
小C&天天

修学储能 先博后渊


今日诗句