How to set the size of a combo box?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

How to set the size of a combo box?

Lars Paulsen
Hello All,

I have created a combo box and it is nicely displayed in my window.
But it always takes the full window size.

I would like to limit the size in that way, that it always takes 1 row
or better say the height of the text line or font size (1em in CSS

I have only text in the combo box so it is not useful that it spans
several lines.
Thanks for all help in advance.

Best Regards,

gtk-app-devel-list mailing list
[hidden email]
Reply | Threaded
Open this post in threaded view

Re: How to set the size of a combo box?

Gtk+ - Apps Dev mailing list

Hi Lars,

There are a few things that you can try to see if you can get the combo box to format how you want it. The first is to create the combo with gtk_combo_box_new_with_model(). That way you can setup your cell renderer and be able to set properties of how the combo box is going to be rendered. To help with the placement of the combo box widget you can use the widget expand and align functions. If you put the combo box in a box container then you have a few more things you can use for getting the alignment correct.


//gcc -Wall combo_size1.c -o combo_size1 `pkg-config --cflags --libs gtk+-3.0`


int main(int argc, char *argv[])
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Combo Size");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 100);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkTreeIter iter;
    GtkListStore *store=gtk_list_store_new(1, G_TYPE_STRING);
    gtk_list_store_append(store, &iter);
    gtk_list_store_set(store, &iter, 0, "Arial 20", -1);
    gtk_list_store_append(store, &iter);
    gtk_list_store_set(store, &iter, 0, "Arial 20, an extra long line to clip", -1);

    GtkCellRenderer *renderer=gtk_cell_renderer_text_new();
    g_object_set(renderer, "height", 30, "width", 200, "font", "Arial 20", NULL);

    GtkWidget *combo=gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
    gtk_widget_set_hexpand(combo, FALSE);
    gtk_widget_set_vexpand(combo, FALSE);
    gtk_widget_set_halign(combo, GTK_ALIGN_END);
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), GTK_CELL_RENDERER(renderer), "text", 0, NULL);


    GtkWidget *box=gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(box), combo, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), box);
    return 0;  

gtk-app-devel-list mailing list
[hidden email]