代码无法从 GTK 应用程序退出 - 显然没有消息循环
Code cannot exit from GTK application - apparently no message loops
我正在尝试弄清楚如何让 GTK 应用程序在之后自动退出
某个时候。
所以我有一个用于关闭应用程序的回调函数:
static gboolean killOffApp (gpointer userData) {
gtk_main_quit ();
return FALSE;
}
然后,在 activate
信号处理程序中,我启动了五秒计时器:
static void activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new (app);
g_timeout_add_seconds (5, killOffApp, NULL);
gtk_widget_show_all (window);
}
而且,为了完整起见,这里是附加信号处理程序的 main
和 运行 GTK 应用程序:
int main (int argc, char **argv) {
GtkApplication *app = gtk_application_new ("com.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
int status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
我的问题是,当计时器触发时,对 gtk_main_quit
的调用似乎无效,我不确定为什么:
(MyProg:776): Gtk-CRITICAL **: gtk_main_quit: assertion 'main_loops != NULL' failed
我已经在网上研究了这个问题,但只发现了以下任一问题:
- 它是从代码的一部分完成的,那里有一个内部循环 运行ning(例如在一个对话框中)所以它关闭了那个循环而不是主循环(这里不是这种情况,我相信);或者
- 没有循环 运行ning 因为,例如,应用程序是 运行 一次性
gtk_main_iteration_do
(我也不认为这里是这种情况)。
我显然做错了某些事情,为什么我的应用程序似乎没有消息循环运行ning?
gtk_main_quit
函数似乎不适合使用 GtkApplication。来自参与各种 Gnome 项目开发的人,一个 Emmanuele Bassi commented elsewhere(稍微解释并特别注意第二段):
If you're calling g_application_run()
then you don't need to call gtk_main()
as well: the run()
method will spin the main loop for you.
You also don't use gtk_main_quit()
to stop the application's main loop: you should use g_application_quit()
instead.
考虑到这一点,您需要将应用程序传递给回调,并用它调用应用程序退出函数:
static void activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new (app);
g_timeout_add_seconds (forceShutdown, killOffApp, app); // << here
gtk_widget_show_all (window);
}
static gboolean killOffApp (gpointer userData) {
g_application_quit (userData); // << and here
return FALSE;
}
我正在尝试弄清楚如何让 GTK 应用程序在之后自动退出 某个时候。
所以我有一个用于关闭应用程序的回调函数:
static gboolean killOffApp (gpointer userData) {
gtk_main_quit ();
return FALSE;
}
然后,在 activate
信号处理程序中,我启动了五秒计时器:
static void activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new (app);
g_timeout_add_seconds (5, killOffApp, NULL);
gtk_widget_show_all (window);
}
而且,为了完整起见,这里是附加信号处理程序的 main
和 运行 GTK 应用程序:
int main (int argc, char **argv) {
GtkApplication *app = gtk_application_new ("com.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
int status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
我的问题是,当计时器触发时,对 gtk_main_quit
的调用似乎无效,我不确定为什么:
(MyProg:776): Gtk-CRITICAL **: gtk_main_quit: assertion 'main_loops != NULL' failed
我已经在网上研究了这个问题,但只发现了以下任一问题:
- 它是从代码的一部分完成的,那里有一个内部循环 运行ning(例如在一个对话框中)所以它关闭了那个循环而不是主循环(这里不是这种情况,我相信);或者
- 没有循环 运行ning 因为,例如,应用程序是 运行 一次性
gtk_main_iteration_do
(我也不认为这里是这种情况)。
我显然做错了某些事情,为什么我的应用程序似乎没有消息循环运行ning?
gtk_main_quit
函数似乎不适合使用 GtkApplication。来自参与各种 Gnome 项目开发的人,一个 Emmanuele Bassi commented elsewhere(稍微解释并特别注意第二段):
If you're calling
g_application_run()
then you don't need to callgtk_main()
as well: therun()
method will spin the main loop for you.You also don't use
gtk_main_quit()
to stop the application's main loop: you should useg_application_quit()
instead.
考虑到这一点,您需要将应用程序传递给回调,并用它调用应用程序退出函数:
static void activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new (app);
g_timeout_add_seconds (forceShutdown, killOffApp, app); // << here
gtk_widget_show_all (window);
}
static gboolean killOffApp (gpointer userData) {
g_application_quit (userData); // << and here
return FALSE;
}