为什么子功能不会破坏GtkWindow? [英] Why does the subfunction not destroy the GtkWindow?

查看:51
本文介绍了为什么子功能不会破坏GtkWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 void window_first();
 void enter_window2(GtkWidget* w, gpointer data);
 void quit(GtkWidget* w, gpointer data);
 void quit();

 int main(int argc, char* argv[])
 {
   GtkWidget* window2;
   gtk_init(&argc, &argv);

   window_first();

   window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_widget_show_all(window2);

   g_signal_connect(G_OBJECT(window2), "destroy", G_CALLBACK(gtk_main_quit), NULL);

   gtk_main();
   return 0;
}
void quit(GtkWidget* w, gpointer data)
{
  exit(1);
}

void enter_window2(GtkWidget* w, gpointer data)
{
  gtk_main_quit();
}

void window_first()
{
   GtkWidget* window1,  *vbox, *enter_window2_button;

   window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   enter_window2_button = gtk_button_new_with_label("enter_window2");

   vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);

   gtk_box_pack_start(GTK_BOX(vbox),enter_window2_button, TRUE, TRUE, 0);
   gtk_container_add(GTK_CONTAINER(window1), vbox);

   g_signal_connect(G_OBJECT(window1), "destroy", G_CALLBACK(quit), NULL);
   g_signal_connect(G_OBJECT(enter_window2_button), "clicked", G_CALLBACK(enter_window2), NULL);

   gtk_widget_show_all(window1);

   gtk_main();
   return;

}

我的代码的目的是构造一个名为"window1"的GtkWindow,它首先具有一个名为enter_window2的GtkButton,并在window1被破坏之后构造另一个名为"window2"的GtkWindow.我希望通过单击"enter_window2"按钮来破坏window1.但是,当我运行代码时.事情没有按预期进行.换句话说,当我按下按钮时,显示了window2,但是window1没有被破坏.那么如何解决呢?

the purpose of my code is to construct a GtkWindow called "window1" which has a GtkButton called enter_window2 first and construct another GtkWindow called "window2" after the window1 was destroyed. I expect the window1 being destroyed by clicking the "enter_window2" button. However, when I run the code. things goes not as expected. In another word, when I press the button, the window2 is displayed, but the window1 is not destroyed. So how to solve it?

推荐答案

为此,您不需要嵌套的主循环,也不应该使用它们:它们是一种反模式,而GTK正在远离甚至允许他们使用(GTK 4正在删除gtk_dialog_run(),甚至可能不允许您自己滚动").

You don't need nested main loops for this, and you shouldn't use them anyway: they are an anti-pattern, and GTK is moving away from even allowing them (GTK 4 is removing gtk_dialog_run() and might not even let you 'roll your own').

您可以并且应该仅使用信号来执行此操作.您只需要使它们正确即可.主要是:在切换到window2之前,我们必须销毁window1上的::destroy处理程序,然后再销毁它.否则,这很容易:

You can and should simply use signals to do this. You just need to get them right. Chiefly: we must disconnect the ::destroy handler on window1 before we destroy it as part of switching to window2. Otherwise, it's easy:

#include <gtk/gtk.h>

void window_first();
void enter_window2(GtkWidget* w, gpointer data);
void window_second();

int main(int argc, char* argv[])
{
    gtk_init(&argc, &argv);
    window_first();
    gtk_main();
    return 0;
}

void enter_window2(GtkWidget* w, gpointer data)
{
    GtkWindow *window1 = GTK_WINDOW(data);
    g_signal_handlers_disconnect_by_func(window1, gtk_main_quit, NULL);
    gtk_widget_destroy(GTK_WIDGET(window1));
    window_second();
}

void window_first()
{
    GtkWidget *window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget *enter_window2_button = gtk_button_new_with_label("enter_window2");
    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);

    gtk_box_pack_start(GTK_BOX(vbox),enter_window2_button, TRUE, TRUE, 0);
    gtk_container_add(GTK_CONTAINER(window1), vbox);

    g_signal_connect(window1, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(enter_window2_button, "clicked", G_CALLBACK(enter_window2), window1);

    gtk_widget_show_all(window1);
}

void window_second()
{
    GtkWidget *window2 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_show_all(window2);

    g_signal_connect(window2, "destroy", G_CALLBACK(gtk_main_quit), NULL);
}

请注意,对于g_signal_connect()或其他各种函数",您无需强制转换为GOBject*,它们实际上是为您检查的宏.

Note that you don't need to cast to GOBject* for g_signal_connect() or various other 'functions', which are really macros that check that for you.

这篇关于为什么子功能不会破坏GtkWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆