完全受够了获得基于GTK控件的高度和宽度 [英] Totally fed-up with get Gtk widget height and width

查看:581
本文介绍了完全受够了获得基于GTK控件的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让身高和GtkEventBox的宽度。结果
尝试下面的事情。

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
--------------------------------------------------------------------------

这实在是不好说的Gtk没有提供简单的功能,这将使你的实际显示高度和小部件的。

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

任何人都试图得到高度和GtkWidget的用?

Anyone tried to get height and with of GtkWidget?

推荐答案

在你的部件都已经实现(在给定大小取决于它的父容器可以给它),你应该能够<$ C $来获取这些值C>小窗口&GT; allocation.width 和小窗口方式&gt; allocation.height

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.

有什么错的方式GTK做到这一点。有什么大小之间的小部件想有什么大小实际上得到的差异。等读取这些值的时序是非常重要的。有'得到'这些变量的方法不会改变的事实,他们尚未初始化。

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,大小分配,G_CALLBACK(my_getsize),NULL);

这篇关于完全受够了获得基于GTK控件的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆