Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

c - Totally fed-up with get Gtk widget height and width

Trying to get Height and Width of GtkEventBox.
Tried following Things.

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); // Again getting 0
--------------------------------------------------------------------------

It is really bad that Gtk has not provided simple function that will give you the actual displayed height and with of the widget.

Anyone tried to get height and with of GtkWidget?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", allocation->width, allocation->height);
}

And in your main loop somewhere, connect the signal:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...