Closed. This question needs details or clarity。它当前不接受答案。
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
4年前关闭。
我正在尝试解决此问题,当我单击“ button1”时,将调用“ on_draw_event”功能(有效),并且在“ darea” -drawing_area中显示一行(无效)。
这是我的代码:
如果我能
“ g_signal_connect(button1,” clicked“,G_CALLBACK(on_draw_event),darea);”
至
“ g_signal_connect(G_OBJECT(darea),”绘制“,G_CALLBACK(on_draw_event),NULL);”
线将被绘制,但我希望它等待我的按钮单击。希望有人可以阻止我,这不是大问题。
谢谢。
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
4年前关闭。
我正在尝试解决此问题,当我单击“ button1”时,将调用“ on_draw_event”功能(有效),并且在“ darea” -drawing_area中显示一行(无效)。
这是我的代码:
#include <gtk/gtk.h>
static gboolean on_draw_event(GtkWidget *button, cairo_t *darea)
{
printf ("function: on_draw_event\n");
cairo_set_source_rgb(darea, 0, 255, 0);
cairo_set_line_width(darea, 0.5);
cairo_move_to(darea, 0, 100);
cairo_line_to(darea, 400, 100);
cairo_stroke(darea);
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
GtkWidget *myGrid;
GtkWidget *button1;
GtkWidget *button2;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_POPUP);
myGrid = gtk_grid_new ();
darea = gtk_drawing_area_new();
gtk_widget_set_size_request(darea, 400, 400);
button1 = gtk_button_new_with_label ("BUTTON 1");
button2 = gtk_button_new_with_label ("QUIT");
gtk_grid_attach(GTK_GRID(myGrid), button1, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(myGrid), darea, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(myGrid), button2, 0, 2, 1, 1);
gtk_container_add(GTK_CONTAINER(window), myGrid);
g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(button1, "clicked", G_CALLBACK(on_draw_event), darea);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
如果我能
“ g_signal_connect(button1,” clicked“,G_CALLBACK(on_draw_event),darea);”
至
“ g_signal_connect(G_OBJECT(darea),”绘制“,G_CALLBACK(on_draw_event),NULL);”
线将被绘制,但我希望它等待我的按钮单击。希望有人可以阻止我,这不是大问题。
谢谢。
最佳答案
如前所述,由于将图形回调连接到clicked
信号,因此示例崩溃。调用时,回调将获得荒谬的参数并死亡。
要“等待按钮单击”,请引入一个标志,告诉您按钮是否已单击。在on_draw_event
中查询该标志,并在按钮的clicked
回调中进行设置。
修改为实现上述示例的示例如下所示:
#include <gtk/gtk.h>
static gboolean on_draw_event(GtkWidget *darea, cairo_t *cr,
gboolean *draw_line)
{
if (!*draw_line)
return FALSE;
printf ("function: on_draw_event\n");
cairo_set_source_rgb(cr, 0, 255, 0);
cairo_set_line_width(cr, 0.5);
cairo_move_to(cr, 0, 100);
cairo_line_to(cr, 400, 100);
cairo_stroke(cr);
return FALSE;
}
static void on_button1_clicked(GtkWidget *button, gboolean *draw_line)
{
*draw_line = TRUE;
/* Make sure the widget is repainted after the click.
gtk_widget_queue_draw() should better be invoked with just the
drawing area, but this code accesses the common parent for
simplicity. Passing darea in a struct whose address is provided as
user_data (along with gboolean *draw_line) is left as exercise. */
gtk_widget_queue_draw(gtk_widget_get_parent(button));
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
GtkWidget *myGrid;
GtkWidget *button1;
GtkWidget *button2;
gboolean draw_line = FALSE;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_POPUP);
myGrid = gtk_grid_new ();
darea = gtk_drawing_area_new();
gtk_widget_set_size_request(darea, 400, 400);
button1 = gtk_button_new_with_label ("BUTTON 1");
button2 = gtk_button_new_with_label ("QUIT");
gtk_grid_attach(GTK_GRID(myGrid), button1, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(myGrid), darea, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(myGrid), button2, 0, 2, 1, 1);
gtk_container_add(GTK_CONTAINER(window), myGrid);
g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(button1, "clicked", G_CALLBACK(on_button1_clicked), &draw_line);
g_signal_connect(G_OBJECT(darea), "draw", G_CALLBACK(on_draw_event), &draw_line);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
关于c - GTK3开罗g_signal_connect:不明白这一点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34656461/
10-11 23:28