我有一个带有菜单的主窗口,它打开另一个窗口。此辅助窗口有一个关闭按钮。该按钮点击了连接的信号。我的问题是我不知道如何关闭/销毁该父窗口。我尝试过gtk_widget_destroy,但是因为窗口不是小部件而出现错误....我没有找到任何破坏父窗口的函数....
请问有人给我指路吗? 提前谢谢。
-------------------------------------------- ---
确定。我发布了一段代码。当我执行程序时,单击“打开窗口”按钮。打开一个新窗口,单击“关闭”按钮。如果我点击“关闭”按钮我在终端中得到下一个错误:(Windows:13801):Gtk-CRITICAL **:gtk_widget_destroy:断言`GTK_IS_WIDGET(小部件)'失败
代码是:
#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”的“clicked”事件按钮的句柄。
如果有人想要执行它,请链接到glade文件:https://sites.google.com/site/marvalsiteimages/secondWindow.glade
我希望这可以帮助你理解我的问题。 Thansk。
-------------------------------------------- -----
基于响应的最终代码:
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);
}
答案 0 :(得分:4)
您的问题是第二个窗口按钮的“单击”信号是从空档文件连接的。但是信号处理程序需要一个指向窗口的指针来销毁它。这是通过信号回调的“user_data”参数传递的。
一种方法是将第二个窗口作为Glade中的user_data参数传递(看一下这个Glade tutorial),但是参数应该是一个指针,我不知道怎么能用林间空地做。 编辑:只需在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);
}