The “button-press-event” signal: button-press-event signal will be emitted when a button(typically f




https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-button-press-event

The “button-press-event” signal

gboolean
user_function (GtkWidget *widget,
               GdkEvent  *event,
               gpointer   user_data)

The ::button-press-event signal will be emitted when a button(typically from a mouse) is pressed.

To receive this signal, the GdkWindow associated to thewidget needs to enable the GDK_BUTTON_PRESS_MASK mask.

This signal will be sent to the grab widget if there is one.




How to Implement a button-press-event on GtkTable

Searching the web for answers dosen't get me through my problem:

I wan't my GtkTable to throw an event, if i click one cell.

Since there is no click event, accept for GtkButton's, i wanted to implement a GDK_BUTTON_PRESS_MASK and GDK_BUTTON_RELEASE_MASK to catch the position of the mouse on the Table during click. Works great with GtkDrawingArea!

Tryed the snipet bellow, but nothing happend, maybe someone can give me a clue :)

little sample:

static void table_press(GtkWidget *widget, GdkEventButton *event)
{
    printf("table pressed");
} 

int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget* table;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW (window), "table click");

    table = gtk_table_new(2, 5, TRUE);

    gtk_container_add(GTK_CONTAINER (window), table);

    gtk_widget_add_events(table, GDK_BUTTON_PRESS_MASK);

    g_signal_connect(GTK_OBJECT (table), "button-press-event",
        G_CALLBACK (table_press), NULL);
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));

    gtk_widget_show_all(window);

    gtk_main();
    main_exit();
    return 0;
}
share | improve this question
 
 
What exactly are you trying to achieve? GtkTable is a container to hold other widgets, so they would most likely get the signal. If there are no widgets attached, the table size might be (0,0). –  Ancurio Mar 14 '13 at 17:53
up vote 1 down vote accepted

You don't receive events because GtkTable does not have a GdkWindow associated with it. You can use GtkEventBox which lets you accept events on widgets that would not normally accept events. This is derived from GtkBin so the interesting code would look like this.

table = gtk_table_new(2, 5, TRUE);
event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER (window), event_box);
gtk_container_add(GTK_CONTAINER (event_box), table);
g_signal_connect(GTK_OBJECT (event_box), "button-press-event",
    G_CALLBACK (table_press), NULL);
share | improve this answer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值