Go-Qt5开发之表格布局(10)

Go-Qt5开发之表格布局

案例说明

  • 表格布局将空间划分为行和列。
  • 我们使用QGridLayout类创建一个网格布局。
  • 在我们的示例中,我们创建一个网格的按钮并且对按钮名称进行填充。

demo.go

package main

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

/*
表格布局将空间划分为行和列。我们使用QGridLayout类创建一个网格布局。

在我们的示例中,我们创建一个网格的按钮。
*/

func InitUi() *widgets.QMainWindow {
	// 创建窗口
	app := widgets.NewQMainWindow(nil, core.Qt__Widget)

	// 设置窗口的标题
	app.SetWindowTitle("Qt 教程")

	// 设置窗口的位置和大小
	app.SetGeometry2(300, 300, 300, 220)

	// 设置窗口的图标,引用当前目录下的web.png图片
	app.SetWindowIcon(gui.NewQIcon5("images/app.ico"))

	// 布局窗口组件载体
	layoutWidget := widgets.NewQWidget(app, core.Qt__Widget)
	//layoutWidget.SetGeometry(core.NewQRect4(300, 300, 300, 220))
	layoutWidget.SetGeometry2(0, 0, 300, 220)
	app.SetCentralWidget(layoutWidget)

	// 表格布局
	grid := widgets.NewQGridLayout(layoutWidget)
	grid.SetContentsMargins(0, 0, 0, 0)

	names := []string{
		"Cls", "Bck", "", "Close",
		"7", "8", "9", "/",
		"数据处理", "5", "6", "*",
		"1", "2", "3", "-",
		"0", ".", "=", "+"}

	// 我们创建一个网格中的位置的列表
	var positions [20]interface{}

	k := 0
	for i := 0; i < 5; i++ {
		for j := 0; j < 4; j++ {
			item := [2]int{i, j}
			positions[k] = item
			k += 1
			//fmt.Println("item: ", item)
		}
		//fmt.Printf("\n")
	}
	//fmt.Println(positions)

	// 创建按钮并使用 AddWidget()方法添加到布局中。
	for index,_ := range positions{
		//fmt.Println(names[index], positions[index])
		button := widgets.NewQPushButton2(names[index], layoutWidget)
		value := positions[index]
		valueObj := value.([2]int)
		grid.AddWidget2(button, valueObj[0], valueObj[1], 0)
	}

	return app
}

func main() {
	// 创建一个应用程序对象
	// sys.argv参数是一个列表,从命令行输入参数
	widgets.NewQApplication(len(os.Args), os.Args)

	// 初始化窗口
	app := InitUi()

	// 显示组件
	app.Show()

	// 确保应用程序干净的退出
	widgets.QApplication_Exec()
}

发布了94 篇原创文章 · 获赞 52 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/weixin_43968923/article/details/105027880
今日推荐