我如何使用gtk信号退出窗口,然后diolog消息显示问题你确定吗?然后单击是按钮关闭所有窗口? 这是我的代码:
void show_queExit(GtkWidget *widget, gpointer window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"Are you sure to exit?");
gtk_window_set_title(GTK_WINDOW(dialog), "Exit");
gint result = gtk_dialog_run (GTK_DIALOG (dialog));
if (result==GTK_RESPONSE_YES)
{
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit),NULL);
gtk_widget_destroy (dialog);
printf("\n***\n");
}
else
gtk_widget_destroy (dialog);
}
这是我的主要功能: int main(int argc,char ** argv) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkToolItem *exitIcon;
GtkWidget *toolbar;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
exitIcon = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exitIcon, -1);
gtk_window_set_default_size(GTK_WINDOW(window), win_width, win_height);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(G_OBJECT(exitIcon), "clicked",G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
答案 0 :(得分:0)
有点像这样:
static gboolean on_delete_event (GtkWidget *window,
GdkEvent *event,
gpointer data)
{
GtkWidget *dialog;
int answer;
dialog = gtk_message_dialog_new (GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"Are you sure to exit?");
gtk_window_set_title (GTK_WINDOW(dialog), "Exit");
answer = gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
if (answer == GTK_RESPONSE_YES) {
g_print ("Yes\n");
return FALSE;
} else {
g_print ("No\n");
return TRUE;
}
}
然后在main()或一些初始化函数中:
g_signal_connect (window, "delete-event",
G_CALLBACK (on_delete_event), NULL);
有几种方法可以处理app退出 - 您可以从上面的函数调用gtk_main_quit(),或者您可以执行我认为您最初尝试的操作:当用户单击YES时让GTK销毁窗口,并调用gtk_main_quit ()来自destroy信号处理程序。
在任何情况下,如果您希望对话框出现,将退出图标"clicked" signal连接到gtk_main_quit()是错误的:您应该将信号连接到您编写的调用gtk_window_close()的函数。
g_signal_connect (exitIcon, "clicked",
G_CALLBACK (on_exit_clicked), window);