Gtk对象、名称、类型等
我在 Glade 中创建了一个窗口和带有 3 个按钮的 vbox。所有按钮都将“Clicked”事件连接到同一处理程序。处理程序如下所示:
CLICKED_btn(GtkObject *object, gpointer user_data)
{
g_print("CLICKED\n");
}
对于任何按钮的任何单击,CLICKED 都会出现在终端上。 在所有按钮使用相同处理程序的情况下,是否有一种方法可以通过对象或其他方式知道哪个按钮调用事件 Clicked?
I Created a window and vbox with 3 buttons in Glade. All buttons have connected "Clicked" event to same handler. Handler looks like this:
CLICKED_btn(GtkObject *object, gpointer user_data)
{
g_print("CLICKED\n");
}
CLICKED appears on terminal for any click to any button.
Is here a way, through object or other to know which button invokes event Clicked in case where all buttons uses same handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
object
参数指的是生成事件的对象,在您的例子中是按钮。然后你可以使用 gtk_widget_get_name() 或任何其他 GtkObject/GtkWidget/GtkButton 函数来做出改变。更新:
看起来,较新版本的 GTK/Glade 没有将小部件的名称设置为它们的
id
,因此保留默认值,即类型的名称。为了获取对象的id
,您可以使用适用于任何可构建对象的函数gtk_buildable_get_name()
。这样,您将获得
button1
、button2
或您为这些按钮指定的任何名称。请不要使用标签来区分按钮。是的,它有效,但这是一个坏习惯:难以维护,国际化不好,并且违背了 Glade 的主要目的:将界面和代码分开。
The
object
parameter refers to the object that generates the event, in your case the button. Then you can usegtk_widget_get_name()
or any other GtkObject/GtkWidget/GtkButton function to make a difference.UPDATE:
As it seems, newer versions of GTK/Glade do not set the name of the widgets to their
id
, so it is left to the default, that is the name of the type. In order to get theid
of the object you can use the functiongtk_buildable_get_name()
that works with any buildable object.With that you'll get
button1
,button2
or whatever name you put to these buttons.Please, do not use the label to make a difference between the buttons. Yes, it works, but it is a bad habit: hard to maintain, bad with internationalization, and defeats the main purpose of Glade: to have the interface and the code separated.
首先使用文档为 “clicked”信号。
然后您就知道回调的原型应该如下所示:
void on_button_cliked (GtkButton *button, gpointer user_data)
button
参数是接收信号的对象,即。您单击的按钮。First use the documentation to have the right prototype for the "clicked" signal of a GtkButton.
You then know that your callback's prototype should look like:
void on_button_cliked (GtkButton *button, gpointer user_data)
The
button
parameter is the object which received the signal, ie. the button on which you clicked.