Refresh the window after an action in GTK+

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 22:04:49

问题


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

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.

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);

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

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);

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 ...

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

Thanks in advance.


回答1:


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.

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.

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);
    ...
}


来源:https://stackoverflow.com/questions/22352079/refresh-the-window-after-an-action-in-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!