标签上的 Gtk 按钮按下事件
Gtk button-press-event on a Label
是否可以在 GtkLabel 上使用信号 button-press-event
?我尝试注册一个信号但没有任何反应,那么我该怎么做才能让它工作?
可能你需要一个事件框,如果没有,那么你真的需要为此提供一个代码。
这是一个演示:
#include <gtk/gtk.h>
static gboolean button_press_event_clbk ( GtkWidget *widget, GdkEvent *event )
{
g_return_val_if_fail ( GTK_IS_LABEL ( widget ), FALSE );
switch ( event->type )
{
case GDK_BUTTON_PRESS :
printf ( "The Button was Pressed\n" );
return TRUE;
case GDK_BUTTON_RELEASE :
printf ( "\tThe Button was Released\n" );
return TRUE;
default:
return FALSE;
}
return FALSE;
}
int main ( void )
{
/// ***
GtkWidget *window;
GtkWidget *label;
GtkWidget *event_box;
/// ***
gtk_init ( NULL, NULL );
/// *** create a new window
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
g_signal_connect ( window, "delete-event", G_CALLBACK ( gtk_main_quit ), NULL );
/// ***
event_box = gtk_event_box_new();
gtk_container_add ( GTK_CONTAINER ( window ), event_box );
/// *** Create the label
label = gtk_label_new ( "Click here" );
gtk_container_add ( GTK_CONTAINER ( event_box ), label );
/// ***
g_signal_connect_swapped ( event_box, "button-press-event", G_CALLBACK ( button_press_event_clbk ), label );
g_signal_connect_swapped ( event_box, "button-release-event", G_CALLBACK ( button_press_event_clbk ), label );
/// ***
gtk_widget_show_all ( window );
/// ***
gtk_main ();
}
输出:
The Button was Pressed
The Button was Released
是否可以在 GtkLabel 上使用信号 button-press-event
?我尝试注册一个信号但没有任何反应,那么我该怎么做才能让它工作?
可能你需要一个事件框,如果没有,那么你真的需要为此提供一个代码。
这是一个演示:
#include <gtk/gtk.h>
static gboolean button_press_event_clbk ( GtkWidget *widget, GdkEvent *event )
{
g_return_val_if_fail ( GTK_IS_LABEL ( widget ), FALSE );
switch ( event->type )
{
case GDK_BUTTON_PRESS :
printf ( "The Button was Pressed\n" );
return TRUE;
case GDK_BUTTON_RELEASE :
printf ( "\tThe Button was Released\n" );
return TRUE;
default:
return FALSE;
}
return FALSE;
}
int main ( void )
{
/// ***
GtkWidget *window;
GtkWidget *label;
GtkWidget *event_box;
/// ***
gtk_init ( NULL, NULL );
/// *** create a new window
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
g_signal_connect ( window, "delete-event", G_CALLBACK ( gtk_main_quit ), NULL );
/// ***
event_box = gtk_event_box_new();
gtk_container_add ( GTK_CONTAINER ( window ), event_box );
/// *** Create the label
label = gtk_label_new ( "Click here" );
gtk_container_add ( GTK_CONTAINER ( event_box ), label );
/// ***
g_signal_connect_swapped ( event_box, "button-press-event", G_CALLBACK ( button_press_event_clbk ), label );
g_signal_connect_swapped ( event_box, "button-release-event", G_CALLBACK ( button_press_event_clbk ), label );
/// ***
gtk_widget_show_all ( window );
/// ***
gtk_main ();
}
输出:
The Button was Pressed
The Button was Released