案例说明
- 表格布局将空间划分为行和列。
- 我们使用QGridLayout类创建一个网格布局。
- 在我们的示例中,我们创建一个网格的按钮并且对按钮名称进行填充。
demo.go
package main
import (
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
"os"
)
func InitUi() *widgets.QMainWindow {
app := widgets.NewQMainWindow(nil, core.Qt__Widget)
app.SetWindowTitle("Qt 教程")
app.SetGeometry2(300, 300, 300, 220)
app.SetWindowIcon(gui.NewQIcon5("images/app.ico"))
layoutWidget := widgets.NewQWidget(app, core.Qt__Widget)
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
}
}
for index,_ := range positions{
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() {
widgets.NewQApplication(len(os.Args), os.Args)
app := InitUi()
app.Show()
widgets.QApplication_Exec()
}