如何使用Gtk::Grid放置小部件,类似于Gtkmm-3中的Gtk::Table

How to place widgets with Gtk::Grid similarly to Gtk::Table in Gtkmm-3

本文关键字:Gtk 类似于 Gtkmm-3 中的 Table 何使用 Grid 小部      更新时间:2023-10-16

最初我学的是pyGtk。几个月后,我决定改用Gtkmm。阅读官方文档,它告诉我Gtk::Table已弃用,我应该使用Gtk::Grid。
如果我想把小部件放在第2行和第3行之间,第4列和第5列之间,我可以在Gtk::Table中使用下面的代码。

// Create variable.        
Gtk::Table table;
//Initiate table.
table(10,10,true);
//Place the widget in the proper position.
// table.attach(widget, left_attach, right_attach, top_attach, bottom_attach);
table.attach(widget,4,5,2,3);

在阅读了Gtk::Grid之后,我尝试了同样的事情,但这次使用了Gtk::Grid。

// Create variable.        
Gtk::Grid grid;
//Place the widget in the proper position.
//grid.attach(widget, left, top, higth, width); 
grid.attach(widget,4,2,1,1);

但是当我尝试最后一个代码的小部件(一个按钮),仍然放在左上角。有人知道我哪里做错了吗?Gtk::Grid有能力做我想做的事情吗?

按钮保持在左上角,因为在第一行或前三列中没有其他小部件占用任何空间,所以它们折叠为零。

可能你想要的是

grid.set_row_homogeneous(true);
grid.set_column_homogeneous(true);

然后在位置0,0和位置9,9附加空白小部件,以确保网格有10行和10列。