如何在 gtk2 中刷新 cairo 绘图?
How to refresh cairo drawing in gtk2?
我在刷新 cairo 绘图时遇到问题。它不起作用,我不知道为什么。你可以帮帮我吗?我想在执行分析功能时创建一个新的 "drawing"。
我创建了这样的东西:
d_area = gtk_drawing_area_new();
然后我把它打包成滚动的window:
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(formula_scrolled_window), d_area);
然后:
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL);
但在它之前我有:
g_signal_connect(G_OBJECT(search_entry), "activate", G_CALLBACK(analyse), &window_and_search_entry);
功能分析:
ELEMENT *analyse(GtkWidget *widget, WINDOW_AND_SEARCH_ENTRY *data)
{
const gchar *text = gtk_entry_get_text(GTK_ENTRY(data->search_entry));
main_window = data->window;
d_area = GTK_DRAWING_AREA(data->d_area); //do I nedd it?
foo = operate(text, GTK_WIDGET(data->window));
print_on_console_with_new_line(foo);
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL); //do I need it??
do_drawing(cr, foo); //is it good
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL); //do I need it??
}
并且:
gboolean expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
cr = gdk_cairo_create (gtk_widget_get_window (widget));
do_drawing(cr, foo);
return FALSE;
}
最后:
void do_drawing(cairo_t *cr, ELEMENT *p)
{
gdouble font_size = 20.0;
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, font_size);
cairo_move_to(cr, lala, 30.0);
cairo_show_text(cr, "6");
lala += 30;
}
尝试在 do_drawing()
函数的开头添加这个
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_paint(cr);
同时对您的 gtk 小部件执行此操作以更新它
gtk_widget_queue_draw(formula_scrolled_window);
我在刷新 cairo 绘图时遇到问题。它不起作用,我不知道为什么。你可以帮帮我吗?我想在执行分析功能时创建一个新的 "drawing"。
我创建了这样的东西:
d_area = gtk_drawing_area_new();
然后我把它打包成滚动的window:
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(formula_scrolled_window), d_area);
然后:
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL);
但在它之前我有:
g_signal_connect(G_OBJECT(search_entry), "activate", G_CALLBACK(analyse), &window_and_search_entry);
功能分析:
ELEMENT *analyse(GtkWidget *widget, WINDOW_AND_SEARCH_ENTRY *data)
{
const gchar *text = gtk_entry_get_text(GTK_ENTRY(data->search_entry));
main_window = data->window;
d_area = GTK_DRAWING_AREA(data->d_area); //do I nedd it?
foo = operate(text, GTK_WIDGET(data->window));
print_on_console_with_new_line(foo);
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL); //do I need it??
do_drawing(cr, foo); //is it good
g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_event_callback), NULL); //do I need it??
}
并且:
gboolean expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
cr = gdk_cairo_create (gtk_widget_get_window (widget));
do_drawing(cr, foo);
return FALSE;
}
最后:
void do_drawing(cairo_t *cr, ELEMENT *p)
{
gdouble font_size = 20.0;
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, font_size);
cairo_move_to(cr, lala, 30.0);
cairo_show_text(cr, "6");
lala += 30;
}
尝试在 do_drawing()
函数的开头添加这个
cairo_set_source_rgb(cr, 1., 1., 1.);
cairo_paint(cr);
同时对您的 gtk 小部件执行此操作以更新它
gtk_widget_queue_draw(formula_scrolled_window);