5

我有一个带有菜单的主窗口,它可以打开另一个窗口。这个辅助窗口有一个关闭按钮。该按钮已连接单击的信号。我的问题是我不知道如何关闭/销毁该父窗口。我尝试过使用 gtk_widget_destroy,但出现错误,因为窗口不是小部件 .... 我还没有找到任何销毁父窗口的函数 ....

谁能给我指路,好吗?提前致谢。

-----------------------------------------------------------

好的。我贴一段代码。当我执行程序时,我单击“打开窗口”按钮。使用“关闭”按钮打开一个新窗口。如果我单击“关闭”按钮,我会在终端中收到下一个错误:(Windows:13801): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed

代码是:

#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

void open_window(GtkWidget *widget, gpointer window);
void close_window(GtkWidget *widget, gpointer window);

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *fixed;
    GtkWidget *button;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Windows");
    gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    fixed = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window), fixed);

    button = gtk_button_new_with_label("Open window");

    gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50);
    gtk_widget_set_size_request(button, 80, 35);

    g_signal_connect(G_OBJECT(button), "clicked",
                     G_CALLBACK(open_window), G_OBJECT(window));

    g_signal_connect_swapped(G_OBJECT(window), "destroy",
                             G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;

}


void open_window(GtkWidget *widget, gpointer window)
{
    GtkBuilder *builder;
    GtkWidget *secondWindow = NULL;

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "secondWindow.glade", NULL);

    secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow"));

    gtk_builder_connect_signals (builder, NULL);

    g_object_unref (G_OBJECT (builder));

    gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE);
    gtk_widget_show_all(secondWindow);
}

void close_window(GtkWidget *widget, gpointer window)
{
    gtk_widget_destroy(GTK_WIDGET(window));
}

在文件“secondWindow.glade”中定义了一个窗口、一个表格和一个放置在表格中间单元格中的按钮。此外,它还为名为“close_window”的“单击”事件按钮定义了一个句柄。

如果有人想执行它,请链接到 glade 文件:https ://sites.google.com/site/marvalsiteimages/secondWindow.glade

我希望这可以帮助您理解我的问题。谢谢。

-------------------------------------------------

基于响应的最终代码:

void open_window(GtkWidget *widget, gpointer window)
{
    GtkBuilder *builder;
    GtkWidget *secondWindow = NULL;
    GtkWidget *closeButton = NULL;

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "secondWindow.glade", NULL);

    secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow"));
    closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton"));

    g_signal_connect (G_OBJECT (closeButton),
                      "clicked",
                      G_CALLBACK (close_window),
                      G_OBJECT (secondWindow)); // here is the magic: the callback will get the window to close

    g_object_unref (G_OBJECT (builder));

    gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE);
    gtk_widget_show_all(secondWindow);
}
4

1 回答 1

5

您的问题是第二个窗口的按钮的“单击”信号是从 glade 文件连接的。但是信号处理程序需要一个指向窗口的指针来销毁它。这是通过信号回调的“user_data”参数传递的。

一种方法是在 Glade 中将第二个窗口作为 user_data 参数传递(看看这个Glade 教程),但该参数应该是一个指针,我不知道如何使用 glade 来做到这一点。编辑:只需在glade 中单击与此信号关联的用户数据字段,弹出窗口将允许您选择要传递给信号处理程序的对象。只需选择您的“secondWindow”对象。

另一种方法是从 glade 文件中删除信号处理,并从代码中手动连接单击的信号,将指向第二个窗口的指针作为用户数据传递:

void open_window(GtkWidget *widget, gpointer window)
{
    GtkBuilder *builder;
    GtkWidget *secondWindow = NULL;
    GtkWidget *closeButton = NULL;

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "secondWindow.glade", NULL);

    secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow"));
    closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton"));

    g_signal_connect (G_OBJECT (closeButton),
        "clicked",
        G_CALLBACK (close_window),
        G_OBJECT (secondWindow)); // here is the magic: the callback will get the window to close

    g_object_unref (G_OBJECT (builder));

    gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE);
    gtk_widget_show_all(secondWindow);
}
于 2012-09-28T16:02:01.127 回答