C语言 完全受够了 get Gtk 小部件的高度和宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2675514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:14:06  来源:igfitidea点击:

Totally fed-up with get Gtk widget height and width

cgtk

提问by User7723337

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

试图获取 GtkEventBox 的高度和宽度。
尝试以下事情。

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.

Gtk 没有提供简单的函数来为您提供实际显示的高度和小部件的大小,这真的很糟糕。

Anyone tried to get height and with of GtkWidget?

有没有人试图通过 GtkWidget 获得高度?

回答by joveha

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.widthand widget->allocation.height.

一旦你的小部件被实现(给定大小取决于它的父容器可以给它什么)你应该能够使用widget->allocation.widthand获取这些值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.

gtk 这样做并没有错。小部件想要的大小与它实际获得的大小之间存在差异。所以读取这些值的时机很重要。这些变量的“get”方法不会改变它们尚未初始化的事实。

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

人们解决这个问题的通常方法是利用size-allocate当小部件获得新的实际大小时发出的信号。像这样的东西:

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

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

回答by zebediah49

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 reallyhas as opposed to what it requested.

如果您使用的是 GTK3,并且小部件已经实现,您可以询问它已分配的内容。这样做的优点是它真正拥有的空间而不是它所要求的空间。

    //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);

回答by el.pescado

Use gtk_widget_size_request(), not gtk_widget_get_size_request().

使用 gtk_widget_size_request(),而不是 gtk_widget_get_size_request()。

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

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

回答by unwind

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

尝试收听地图事件信号。