GtkGrid is a container which arranges its child widgets in rows and columns, with arbitrary positions and horizontal/vertical spans.
Children are added using attach. They can span multiple rows or columns. It is also
possible to add a child next to an existing child, using attach_next_to. The
behaviour of GtkGrid when several children occupy the same grid cell is undefined.
GtkGrid can be used like a Box by just using add
, which will place children next to each other in the direction determined by the
orientation property. However, if all you want is a single row or column,
then Box is the preferred widget.
CSS nodes
GtkGrid uses a single CSS node with name grid.
Example: Grid:

public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.Grid";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
// The Grid:
Gtk.Grid grid = new Gtk.Grid();
this.add (grid);
// XX __ __
// __ __ __
// __ __ __
Gtk.Button button1 = new Gtk.Button.with_label ("1");
grid.attach(button1, 0, 0, 1, 1);
// XX __ __
// XX __ __
// XX __ __
Gtk.Button button2 = new Gtk.Button.with_label ("2");
grid.attach(button2, 0, 1, 1, 2);
// XX __ __
// XX XX __
// XX __ __
Gtk.Button button3 = new Gtk.Button.with_label ("3");
grid.attach(button3, 1, 1, 1, 1);
// XX __ __
// XX XX __
// XX __ XX
Gtk.Button button4 = new Gtk.Button.with_label ("4");
grid.attach(button4, 2, 2, 1, 1);
// XX XXXXX
// XX XX __
// XX __ XX
Gtk.Button button5 = new Gtk.Button.with_label ("5");
grid.attach_next_to (button5, button1, Gtk.PositionType.RIGHT, 2, 1);
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}
valac --pkg gtk+-3.0 Gtk.Grid.vala
- public void attach (Widget child, int left, int top, int width = 1, int height = 1)
Adds a widget to the grid.
- public void attach_next_to (Widget child, Widget? sibling, PositionType side, int width = 1, int height = 1)
Adds a widget to the grid.
- public int get_baseline_row ()
Returns which row defines the global baseline of
this.
- public unowned Widget? get_child_at (int left, int top)
Gets the child of this whose area
covers the grid cell whose upper left corner is at left
, top
.
- public bool get_column_homogeneous ()
Returns whether all columns of this
have the same width.
- public uint get_column_spacing ()
Returns the amount of space between the columns of
this.
- public BaselinePosition get_row_baseline_position (int row)
- public bool get_row_homogeneous ()
Returns whether all rows of this
have the same height.
- public uint get_row_spacing ()
Returns the amount of space between the rows of
this.
- public void insert_column (int position)
Inserts a column at the specified position.
- public void insert_next_to (Widget sibling, PositionType side)
Inserts a row or column at the specified position.
- public void insert_row (int position)
Inserts a row at the specified position.
- public void remove_column (int position)
Removes a column from the grid.
- public void remove_row (int position)
Removes a row from the grid.
- public void set_baseline_row (int row)
Sets which row defines the global baseline for the entire grid.
- public void set_column_homogeneous (bool homogeneous)
Sets whether all columns of this
will have the same width.
- public void set_column_spacing (uint spacing)
Sets the amount of space between columns of
this.
- public void set_row_baseline_position (int row, BaselinePosition pos)
Sets how the baseline should be positioned on row
of the
grid, in case that row is assigned more space than is requested.
- public void set_row_homogeneous (bool homogeneous)
Sets whether all rows of this will
have the same height.
- public void set_row_spacing (uint spacing)
Sets the amount of space between rows of
this.