Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (二) UI继承

2023-05-16

简介

    在UI A 中嵌套UI_B, UI_B 是前面搭建的一个UI控件

 

创建 UI 文件

创建UI TextWidget

ObjectName : TextWidget

文件名保存为: textwidget.ui

拖了一个QTextWidget到创建的界面中

创建UI MainWindow, 并使用 TextWidget

使用 Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (一) 环境搭建 中创建的的login.ui

在界面中拖动一个QWidget, 并修改ObjectName为TextWidget

提升TextWidget

编译UI文件 到 Go

1. 将login.ui 和 textwidget.ui放到ui目录中

2. 利用 Goland外部工具 或指令将UI转成go文件

参考 Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (一) 环境搭建  进行外部工具配置

或指令: E:\CodePath\Golang\bin\goqtuic.exe -go-ui-dir=../uitogo -ui-file=ui

微创手术代码整改

textwidget_ui.go

// WARNING! All changes made in this file will be lost!
package ui

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

/* 增加代码 TextWidget */
type TextWidget struct {
	*UITextWidget
	*widgets.QWidget
}

/* 增加代码 NewTextWidget */
func NewTextWidget(parent widgets.QWidget_ITF, ff core.Qt__WindowType) *TextWidget {
	var textWidget = &TextWidget{
		UITextWidget: &UITextWidget{},
		QWidget: widgets.NewQWidget(parent, ff),
	}
	textWidget.UITextWidget.SetupUI(textWidget.QWidget)
	
	return textWidget
}

type UITextWidget struct {
	HorizontalLayout *widgets.QHBoxLayout
	TextEdit *widgets.QTextEdit
}


func (this *UITextWidget) SetupUI(TextWidget *widgets.QWidget) {
	TextWidget.SetObjectName("TextWidget")
	TextWidget.SetGeometry(core.NewQRect4(0, 0, 400, 185))
	this.HorizontalLayout = widgets.NewQHBoxLayout2(TextWidget)
	this.HorizontalLayout.SetObjectName("horizontalLayout")
	this.HorizontalLayout.SetContentsMargins(0, 0, 0, 0)
	this.HorizontalLayout.SetSpacing(0)
	this.TextEdit = widgets.NewQTextEdit(TextWidget)
	this.TextEdit.SetObjectName("TextEdit")
	this.HorizontalLayout.AddWidget(this.TextEdit, 0, 0)


    this.RetranslateUi(TextWidget)

}


func (this *UITextWidget) RetranslateUi(TextWidget *widgets.QWidget) {
    _translate := core.QCoreApplication_Translate
	TextWidget.SetWindowTitle(_translate("TextWidget", "Form", "", -1))
}

login_ui.go // 依葫芦(textwidget_ui.go)画瓢

// WARNING! All changes made in this file will be lost!
package ui

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

/* 增加代码 LoginMainWindow */
type LoginMainWindow struct {
	*UILoginMainWindow
	*widgets.QMainWindow
}

/* 增加代码 NewLoginMainWindow */
func NewLoginMainWindow(parent widgets.QWidget_ITF, ff core.Qt__WindowType) *LoginMainWindow {
	var mainwindow = &LoginMainWindow{
		UILoginMainWindow: &UILoginMainWindow{},
		QMainWindow: widgets.NewQMainWindow(nil, core.Qt__Window),
	}
	mainwindow.UILoginMainWindow.SetupUI(mainwindow.QMainWindow)

	return mainwindow
}

type UILoginMainWindow struct {
	Centralwidget *widgets.QWidget
	VerticalLayout *widgets.QVBoxLayout
	Widget *widgets.QWidget
	GridLayout2 *widgets.QGridLayout
	LineEdit *widgets.QLineEdit
	Label3 *widgets.QLabel
	Label2 *widgets.QLabel
	LineEdit2 *widgets.QLineEdit
	Widget3 *widgets.QWidget
	HorizontalLayout *widgets.QHBoxLayout
	HorizontalSpacer *widgets.QSpacerItem
	PushButton *widgets.QPushButton
	HorizontalSpacer2 *widgets.QSpacerItem
	TextWidget *TextWidget // 修改代码 去掉 widgets.
	Menubar *widgets.QMenuBar
	Statusbar *widgets.QStatusBar
}

func (this *UILoginMainWindow) SetupUI(MainWindow *widgets.QMainWindow) {
	MainWindow.SetObjectName("MainWindow")
	MainWindow.SetGeometry(core.NewQRect4(0, 0, 249, 269))
	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(4, 0, 4, 0)
	this.VerticalLayout.SetSpacing(0)
	this.Widget = widgets.NewQWidget(this.Centralwidget, core.Qt__Widget)
	this.Widget.SetObjectName("Widget")
	this.GridLayout2 = widgets.NewQGridLayout(this.Widget)
	this.GridLayout2.SetObjectName("gridLayout_2")
	this.GridLayout2.SetContentsMargins(0, 0, 0, 0)
	this.GridLayout2.SetSpacing(4)
	this.LineEdit = widgets.NewQLineEdit(this.Widget)
	this.LineEdit.SetObjectName("LineEdit")
	this.GridLayout2.AddWidget3(this.LineEdit, 0, 1, 1, 1, 0)
	this.Label3 = widgets.NewQLabel(this.Widget, core.Qt__Widget)
	this.Label3.SetObjectName("Label3")
	this.GridLayout2.AddWidget3(this.Label3, 1, 0, 1, 1, 0)
	this.Label2 = widgets.NewQLabel(this.Widget, core.Qt__Widget)
	this.Label2.SetObjectName("Label2")
	this.GridLayout2.AddWidget3(this.Label2, 0, 0, 1, 1, 0)
	this.LineEdit2 = widgets.NewQLineEdit(this.Widget)
	this.LineEdit2.SetObjectName("LineEdit2")
	this.LineEdit2.SetEchoMode(widgets.QLineEdit__Password)
	this.GridLayout2.AddWidget3(this.LineEdit2, 1, 1, 1, 1, 0)
	this.Widget3 = widgets.NewQWidget(this.Widget, core.Qt__Widget)
	this.Widget3.SetObjectName("Widget3")
	this.HorizontalLayout = widgets.NewQHBoxLayout2(this.Widget3)
	this.HorizontalLayout.SetObjectName("horizontalLayout")
	this.HorizontalLayout.SetContentsMargins(0, 0, 0, 0)
	this.HorizontalLayout.SetSpacing(0)
	this.HorizontalSpacer = widgets.NewQSpacerItem(40, 20, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Minimum)
	this.HorizontalLayout.AddItem(this.HorizontalSpacer)
	this.PushButton = widgets.NewQPushButton(this.Widget3)
	this.PushButton.SetObjectName("PushButton")
	this.HorizontalLayout.AddWidget(this.PushButton, 0, 0)
	this.HorizontalSpacer2 = widgets.NewQSpacerItem(40, 20, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Minimum)
	this.HorizontalLayout.AddItem(this.HorizontalSpacer2)
	this.GridLayout2.AddWidget3(this.Widget3, 2, 0, 1, 2, 0)
	this.VerticalLayout.AddWidget(this.Widget, 0, 0)
	this.TextWidget = NewTextWidget(this.Centralwidget, core.Qt__Widget) // 修改代码 去掉 widgets. 增加第二参数core.Qt__Widget
	this.TextWidget.SetObjectName("TextWidget")
	this.VerticalLayout.AddWidget(this.TextWidget, 0, 0)
	MainWindow.SetCentralWidget(this.Centralwidget)
	this.Menubar = widgets.NewQMenuBar(MainWindow)
	this.Menubar.SetObjectName("Menubar")
	this.Menubar.SetGeometry(core.NewQRect4(0, 0, 249, 23))
	MainWindow.SetMenuBar(this.Menubar)
	this.Statusbar = widgets.NewQStatusBar(MainWindow)
	this.Statusbar.SetObjectName("Statusbar")
	MainWindow.SetStatusBar(this.Statusbar)


    this.RetranslateUi(MainWindow)

}

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

main.go

package main

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

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

	myui.NewLoginMainWindow().Show()

	widget.Exec()
}

编译 main.go, 

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

问题:

1. 

原因是创建的NewTextWidget 方法 与编译后生成的uic_xx.go 内部的NewTextWidget重名了,qtdeploy 编译时会生成ui目录, 并产生对应的uic_xxx.go

解决方法(两种方法): 

(1) 修改UI编译成go的生成路径, 修改为../uitogo, 已经编译好的, 直接创建新目录放置进去,错开

E:\CodePath\Golang\bin\goqtuic.exe -go-ui-dir=../uitogo -ui-file=ui

(2) 将NewLoginMainwindow 和 NewTextWidget改成 _NewMainWindows 和 _NewTextWidget

 

2. 要是出现 问题, 先在工程目录,即mod文件所在目录执行下面

go mod tidy 或 go mod download

go mod vendor 

再重新进行编译

 

资源

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (二) UI继承 的相关文章

随机推荐

  • 编码格式(关于utf-8,gb2312,gbk,big5等)

    计算机数据是以二进制的方式来存储 xff0c 符号代表文字 那么二进制数据表示的模式就是编码 xff0c 跟电报的加密解密是一个道理 xff0c 那么如何将这些数据转化成有效字符 xff0c 这就涉及到了编码格式 xff0c 一般常见的编码
  • Ubuntu配置桥接网络

    第一步 xff1a 点击虚拟机 xff0c 点击设置 第二步 xff1a 点击网络适配器 xff0c 选中桥接模式并确定 第三步 xff1a 点击编辑 xff0c 打开虚拟网络编辑器 xff0c 选中桥接模式 点击 网络属性 找到描述 xf
  • 基于 NCNN, 实现 yolov8

    记录下 基于 ncnn 实现 yolov8 的全部过程 修改 ultralytics nn modules py class Detect forward 和 class C2f forward span class token keywo
  • HRNet 训练自定义数据集

    基于 HRNet 训练人脸特征点数据集 INSTALL conda create n openmmlab span class token assign left variable python span span class token
  • Chatgpt 指令收集

    在使用 ChatGPT 时 xff0c 当你给的指令越精确 xff0c 它的回答会越到位 xff0c 举例来说 xff0c 假如你要请它帮忙写文案 xff0c 如果没给予指定情境与对象 xff0c 它会不知道该如何回答的更加准确 一 写报告
  • openEuler 安装图形桌面环境Gnome或DDE或UKUI

    由于openEuler系统主要针对服务器 xff0c 目前默认安装之后没有图形桌面环境 xff0c 需要的用户可以自己手动安装配置 这里推荐安装深度桌面DDE或优麒麟UKUI环境 安装gnome桌面 sudo dnf makecache s
  • Ubuntu更换国内镜像源

    由于Ubuntu官方镜像速度有限 xff0c 可以使用国内镜像加速更新和下载 xff0c 节约时间 常用的国内镜像有很多 xff0c 本人常用的有如下几个 xff0c 仅供参考 163镜像 mirrors 163 com 清华镜像 mirr
  • ubuntu-2204 gerrit ssh 报错Permission denied (publickey).分析及解决

    ubuntu 2204 gerrit ssh 报错Permission denied publickey 分析及解决 使用repo init sync下载代码时遇到报错 Permission denied publickey 分析排查步骤
  • 消息序列化工具-protobuf介绍及安装使用技巧

    简介 protobuf是google团队开发的用于高效存储和读取结构化数据的工具 xml json也可以用来存储此类结构化数据 xff0c 但是使用protobuf表示的数据能更加高效 xff0c 并且将数据压缩得更小 xff0c 大约是j
  • 消息序列化工具-为现代C++设计的jsoncpp介绍与使用技巧

    概述 JSON 的全称为 xff1a JavaScript Object Notation xff0c 顾名思义 xff0c JSON 是用于标记 Javascript 对象的 xff0c JSON 官方的解释为 xff1a JSON 是一
  • cppcheck代码检查工具安装与使用技巧

    cppcheck代码检查工具安装与使用技巧 Cppcheck 是一种 C C 43 43 代码缺陷静态检查工具 不同于 C C 43 43 编译器及很多其它分析工具 xff0c 它不检查代码中的语法错误 Cppcheck 可以检查非标准代码
  • sed流编辑器中使用变量替换以及执行外部命令

    在使用sed对日志或者其它文本进行parse的过程当中 xff0c 有时候我们需要引用外部变量的值 xff0c 或者获取一个shell命令执行的结果 xff0c 以便达到更加可观的输出结果 这里介绍如何做到 sed 流编辑 1 sed命令及
  • (计蒜客) 取石子游戏 (gcd算法灵活运用)

    蒜头君和花椰妹在玩一个游戏 xff0c 他们在地上将 n 颗石子排成一排 xff0c 编号为 1 到 n 开始时 xff0c 蒜头君随机取出了 2 颗石子扔掉 xff0c 假设蒜头君取出的 2 颗石子的编号为 a b 游戏规则如下 xff0
  • mkisofs命令制作iso文件

    mkisofs命令行格式 mkisofs adDfhJlLNrRTvz print size quiet A lt 应用程序ID gt b lt 开机映像文件 gt c lt 开机文件名称 gt hide lt 目录或文件名 gt hide
  • windows下tree命令列出文件目录树

    windows下tree命令列出文件目录树 tree path f tree D AR C Team f 可以将D AR C Team目录下所有目录及子目录下的文件都打印出来 tree D AR C Team f gt HOMEPATH f
  • yum命令安装历史回滚彻底删除安装的依赖包

    yum命令安装一个软件包是会连同依赖包一起安装 xff0c 但是yum remove卸载时却只卸载这个文件包本身 如果需要删除安装时附加的依赖包可以使用yum history的相关操作实现回滚 假如安装了ecliipse pde xff0c
  • latex在ipython jupyter notebook中的使用

    In 2 from IPython display import Latex In 5 数学公式的前后要加上 或 和 Latex r 34 f x 61 3x 43 7 34 Out 5 In 6
  • wsl 镜像迁移

    wsl 镜像迁移 1 打开CMD xff0c 查看所有WSL wsl l all v NAME STATE VERSION Ubuntu 20 04 Stopped 2 centos Running 2 2 导出WSL wsl export
  • Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (一) 环境搭建

    Note 开启模块支持 xff0c 设置国内高速代理 xff0c 参考 https www jianshu com p d782d70b3a25 简介 搭建的目的只是刚好看到有这么一个模块 xff0c 还有给使用Go的人需要用到调试界面的时
  • Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (二) UI继承

    简介 在UI A 中嵌套UI B UI B 是前面搭建的一个UI控件 创建 UI 文件 创建UI TextWidget ObjectName TextWidget 文件名保存为 textwidget ui 拖了一个QTextWidget到创