26

To get the height and width of a GtkEventBox, I tried the following:

GtkRequisition requisition;
gtk_widget_get_child_requisition(widget, &requisition);
// Getting requisition.height 0

widget->allocation-x   //getting 0
widget->allocation-height   //getting -1

gtk_widget_get_size_request( widget, &height, &width); //getting 0

What function will give you the actual displayed height and width of the widget?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
User7723337
  • 11,857
  • 27
  • 101
  • 182

4 Answers4

35

Once your widget have been realized (given a size depending on what it's parent container can give it) you should be able to get these values with widget->allocation.width and widget->allocation.height.

There's nothing wrong in the way gtk does this. There's a difference between what size a widget would like to have and what size it actually gets. So the timing on reading these values is important. Having 'get' methods for these variables wont change the fact that they are not initialized yet.

The usual way people go around this is to tap into the size-allocate signal that is emitted when the widget got a new actual size. Something like this:

void my_getsize(GtkWidget *widget, GtkAllocation *allocation, void *data) {
    printf("width = %d, height = %d\n", allocation->width, allocation->height);
}

And in your main loop somewhere, connect the signal:

g_signal_connect(mywidget, "size-allocate", G_CALLBACK(my_getsize), NULL);

joveha
  • 2,599
  • 2
  • 17
  • 19
  • Am I right to guess that the allocation will not be retrievable unless the control has been made visible with `show_all`? (I have to use a `Fixed` layout and want to know the width of widget 1 before adding widget 2) – phil294 Aug 07 '22 at 13:39
10

If you are using GTK3, and the widget has been realized you can ask for what it has been allocated. This has the advantage of being the space that it really has as opposed to what it requested.

    //GtkWidget* widget;
    GtkAllocation* alloc = g_new(GtkAllocation, 1);
    gtk_widget_get_allocation(widget, alloc);
    printf("widget size is currently %dx%d\n",alloc->width, alloc->height);
    g_free(alloc);
zebediah49
  • 7,467
  • 1
  • 33
  • 50
  • 11
    There's no point on dynamically allocating data here, just declare your alloc variable on the stack, not on the heap. – liberforce Nov 08 '12 at 13:47
  • 2
    something like this ? : `GtkAllocation alloc; gtk_widget_get_allocation(widget, &alloc); printf("widget size is currently %dx%d\n",alloc.width, alloc.height);` – revo Aug 27 '13 at 07:09
5

Use gtk_widget_size_request(), not gtk_widget_get_size_request().

http://library.gnome.org/devel/gtk/stable/GtkWidget.html#gtk-widget-size-request

4

Are you sure that your widget has been both shown and realized/mapped? You can't get the size until the widget has been laid out "for real".

Try listening to the map-event signal.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    I am doing `gtk_widget_show` before calling these function. but i thing at that time it is not actually drawn on screen (as there parent has not been shown) but i need these parameters before it get's drawn on screen as i need to pass these values to some other process. so how do i get it? – User7723337 Apr 20 '10 at 13:53
  • @PP: I don't think you can know, before the actual layout happens. Perhaps you can change your design so the other process can receive the coordinates once they are known? – unwind Apr 20 '10 at 14:17