在GTK +中执行操作后刷新窗口 [英] Refresh the window after an action in GTK+

查看:243
本文介绍了在GTK +中执行操作后刷新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发现GTK,并且遇到了多个问题...这是其中之一.

I'm discovering GTK and I have multiples issues ... Here is one of them.

我有一个数据"结构和一个包含菜单栏和drawingArea的窗口. 该区域具有一个绘图功能"DrawRefresh_callback",专用于绘制数据中描述的内容.

I have a "Data" structure and a Window which contains a menu bar and a drawingArea. The area has a drawing function "DrawRefresh_callback" dedicated to draw the content described in Data.

目前,我可以在创建DrawingArea时通过连接点击信号来进行绘制:

At the moment, I can draw by connecting the click signal when I create the DrawingArea :

g_signal_connect(G_OBJECT(DrawingArea), "button-press-event", G_CALLBACK(Draw_callback), pData);

事实上,通过这样做,我可以访问数据和Draw_callback中的DrawingArea小部件.

Indeed, by doing so, I have access to the data AND the DrawingArea widget in Draw_callback.

当我使用菜单时,我能够调用一个名为Data_addLine的函数来修改Data.我在连接"activate"信号时给出了一个指向Data的指针. (我什至不知道这是个好方法).

When I use the menu, I am able to call a function called Data_addLine that modifies Data.I gave a pointer to Data when I connected the "activate" signal in order to do this. (I'm not even sure that's the good way to do it).

g_signal_connect(G_OBJECT(pMenuItem), "activate", G_CALLBACK(Data_addLine), pData);

但是,我想通过从Data_addLine调用DrawRefresh_callback刷新绘图区域.而且我不知道该怎么做:在Data_addLine中,我无法访问drawingWidget(除非使用大量的"gtk_widget_get_parent" ...).

But then, I would like to refresh the drawing area by calling DrawRefresh_callback from Data_addLine. And I don't know how to do this : in Data_addLine I can't access the drawingWidget (except by using a lot of "gtk_widget_get_parent" ...).

我完全迷路了...我什至难以解释我的问题... 希望这足够清楚...

I'm totally lost ... and I even have difficulties explaining my problem ... Hope this is clear enough ...

也许这不是将Gtk与Data结构一起使用的方法...

Maybe this is not the way to use Gtk with a Data struct ...

谢谢.

推荐答案

如果我答对了,您将响应某些事件而更改模型"(=您的数据结构),并希望相应的绘图区域得到更新.

If I got you right, you alter your "model" (=your data structure) in response to some event and want the drawing area to get updated accordingly.

我建议不要从另一个事件处理程序(即从处理菜单激活信号的处理程序)中调用绘制例程,而应加入重绘操作.为此使用gtk_widget_queue_draw(Draw);.

I would recommend not to call the drawing routine from within another event handler (i.e. from the handler that processes the menu-activate signal) but to enqueue a redraw operation instead. Use gtk_widget_queue_draw(Draw); for that.

然后,在事件处理程序中,您需要两个引用.一种是数据结构,另一种是绘图区域的小部件.您可以为此使用全局变量,也可以使用包含所有必需变量的结构(在下面的示例中为appdata),然后可以将其方便地传递给事件处理程序.

Then, in the event handler you need two references. One to your data structure and one to the widget of the drawing area. You can either work with global variables for this, or use a structure holding all necessary variables (appdata in the example below) which you can then pass conveniently to your event handlers.

typedef struct {
    GtkWidget  *DrawingArea;
    sometype_t *Data;
    ...
} appdata_t;

static gboolean on_menuitem_activate(GtkMenuItem *menuitem, appdata_t *appdata)
{
    do_some_work(appdata->Data);
    gtk_widget_queue_draw(appdata->DrawingArea);
    return FALSE;
}

...

int main(...)
{
    appdata_t appdata;

    appdata.DrawingArea = gtk_drawing_area_new();
    appdata.Data = some_initialization_function();

    ...
    g_signal_connect(pMenuItem, "activate", on_menuitem_activate, &appdata);
    ...
}

这篇关于在GTK +中执行操作后刷新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆