Home > Net >  How to center widgets in a grid - GTK C
How to center widgets in a grid - GTK C

Time:06-28

I'm trying to center these buttons in a this grid but I've tried set_hexpand and variants and nothing's what I'm looking for.

GtkWidget *p_main_grid = gtk_grid_new();

   gtk_grid_set_column_spacing(GTK_GRID(p_main_grid), 16);
   gtk_grid_set_row_spacing(GTK_GRID(p_main_grid), 16);

   /* Buttons */

   GtkWidget *p_button_1 = gtk_button_new_with_label("Button 1");
   GtkWidget *p_button_2 = gtk_button_new_with_label("Button 2");

   /* Add Buttons to grid */

   gtk_grid_attach(GTK_GRID(p_main_grid), p_button_1, 5, 3, 1, 1);
   gtk_grid_attach_next_to(GTK_GRID(p_main_grid), p_button_2, p_button_1,
                           GTK_POS_RIGHT, 1, 1);

   /* Add grid to main window */

   gtk_window_set_child(GTK_WINDOW(p_main_window), p_main_grid);
   gtk_window_present(GTK_WINDOW(p_main_window));

I'm on linux and using gtk4

enter image description here

CodePudding user response:

I used this in gtk3 and it centered two buttons.
This tries to use 10 columns and 10 rows.
The spacing of 16 seems to provide some distance between the buttons.

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    //blank label to take up 2 cols by 10 rows on left
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);

    //blank lable to take up 6 cols by 4 rows at top center
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);

    //blank label to take up 2 cols by 10 rows on right
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);

This is working with 4 cols by 3 rows

    gtk_window_set_default_size ( GTK_WINDOW ( window), 400, 200);

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on left
    // gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on left
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 1, 3);

    title = gtk_label_new ( "");
    //blank lable to take up 6 cols by 4 rows at top center
    // gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);
    //blank lable to take up 2 cols by 1 rows at top center
    gtk_grid_attach (GTK_GRID ( grid), title, 1, 0, 2, 1);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    // gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    //buttons 1 cols by 1 row each placed below the top center blank label
    gtk_grid_attach (GTK_GRID ( grid), button1, 1, 1, 1, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    // gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);
    gtk_grid_attach (GTK_GRID ( grid), button2, 2, 1, 1, 1);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on right
    // gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on right
    gtk_grid_attach (GTK_GRID ( grid), title, 3, 0, 1, 3);
  • Related