I am trying to center these buttons in the grid created. I've tried set_halign and variants but nothing's what I'm looking for. Unsure how to format, but please help.
#include <gtk/gtk.h>
static void ttt_activate(GtkApplication *app){
GtkWidget *window = gtk_application_window_new (app);
GtkWidget *grid = gtk_grid_new();
GtkWidget *label = gtk_label_new("Label");
gtk_window_set_default_size (GTK_WINDOW (window), 800, 800);
gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
// Create buttons
GtkWidget * button1 = gtk_button_new_with_label ("button1");
GtkWidget * button2 = gtk_button_new_with_label ("button2");
// Attaching buttons
gtk_grid_attach(GTK_GRID(grid), button1, 5, 3, 2, 3);
gtk_grid_attach(GTK_GRID(grid), button2, 10, 3, 2, 3);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_window_close), window);
g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_window_close), window);
gtk_window_set_child(GTK_WINDOW(window), grid);
gtk_window_present(GTK_WINDOW(window));
}
int main (int argc, char *argv[]) {
GtkApplication *app = gtk_application_new ("com.example.GtkApplication", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (ttt_activate), NULL);
return g_application_run (G_APPLICATION (app), argc, argv);
}
By default, the cells in a GtkGrid only take up as much space as the biggest sibling cell in the column or row actually needs. If you want to have each cell in the GtkGrid take up the exact same amount, you want to set the colum-homogeneous
and row-homogeneous
properties, e.g. by using gtk_grid_set_column_homogeneous()
Once you've set those, you can then set the appropriate column and row to put them in the middle (and halign and valign within their cell)