如何组织 GTK 程序?

问题描述

我有一个名为 create_interface函数。在该函数中是打开菜单按钮的信号处理程序(菜单是在 create_interface 中创建的)。我想传递窗口小部件并传递树视图小部件,因为当您打开文件时,树视图中的条目应该显示出来。我尝试将它们作为结构传递,但是,尽管它有效,但 GTK 会生成一些错误消息。

// This is global.
struct everything
        {
                GtkWidget *window;
                GtkWidget *tree_view;
        }

GtkWidget *create_interface (void)
{
        struct everything instance;
        struct everything *widgets;
        widgets = &instance;
        
        ...code here...

        g_signal_connect(file_mi_open_file_dialog,"clicked",G_CALLBACK(open_file_dialog),widgets);

函数 open_file_dialog 如下所示:

void open_file_dialog (GtkWidget *wid,gpointer data)
{
        struct everything *sample_name = data;
        ...rest of code here...

我想知道是否有另一种组织程序的方法,这样您就不必拥有全局变量

解决方法

我尝试将它们作为结构传递,但是,尽管它有效,但 GTK 会生成一些错误消息。

问题是您正在尝试使用堆栈分配的结构,它在 create_interface() 函数完成后变得无效,而您通常希望这些值在稍后的时间仍然有效(例如,当调用 open_file_dialog() 时。

我想知道是否有另一种组织程序的方法,这样您就不必拥有全局变量。

一种可能的解决方案确实是使用全局变量:这将在程序的整个生命周期内有效,但它有主要缺点:如果您必须为每个回调都这样做,则它不会扩展,并且在架构上不是很干净。

另一种解决方案是在堆上分配“闭包”(即在创建回调时要捕获的变量),并在完成后释放它。 GLib 甚至可以通过调用 g_signal_connect_data()(如果您将字段保存在 GObject 中,则为 g_signal_connect_object()

// Technically you don't need the typedef
typedef struct _ClickedClosure {
    GtkWidget *window;
    GtkWidget *treeview;
} ClickedClosure;

GtkWidget *create_interface (void)
{
    // Note that the g_new0 allocates on the heap instead of using the stack
    ClickedClosure *closure = g_new0 (ClickedClosure,1);

    // code here to initialize your window/treeview ...

    // put the widgets in the closure
    closure->window = my_window;
    closure->treeview = treeview;

    // Connect to the signal
    g_signal_connect_data(file_mi_open_file_dialog,"clicked",G_CALLBACK(open_file_dialog),closure,g_free,0);
}

void open_file_dialog (GtkWidget *wid,gpointer data)
{
    // Since this was allocated on the heap,this is still valid
    ClickedClosure *sample_name = data;

    // code handling here ...
}

很多时候,使用 GTK 的开发人员所做的是他们已经在使用他们自己的 GObject 类/对象,然后他们可以使用 g_signal_connect_object(),或者稍后通过 g_object_unref() 手动释放它.使用 GObject 还允许您进行运行时类型检查转换,这确保您的闭包具有正确的类型。