提问人:dibyendu 提问时间:11/17/2023 最后编辑:dibyendu 更新时间:11/17/2023 访问量:35
如何在窗口focus-out-event上退出GtkApplication?
How to Quit GtkApplication on window focus-out-event?
问:
这是 gtk3 中 widget “focus-out-event” 的一个示例。我的目标是在窗口失去焦点时通过单击窗口外退出窗口。但是组合框(子)小部件有问题。每当我单击组合框时,窗口都会失去焦点并关闭。但是,如果我单击缩放小部件,则没问题。这是脚本——
#include <gtk/gtk.h>
//gcc `pkg-config --cflags gtk+-3.0` -o example Changer3.c`pkg-config --libs gtk+-3.0`
gboolean quit_function (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
GApplication *app=g_application_get_default();
g_application_quit(app);
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
//create Window
GtkWidget *window;
GtkWidget *center_vbox;
GtkWidget *combobox;
GtkWidget *scale;
window = gtk_application_window_new (app);
gtk_window_set_decorated (GTK_WINDOW(window), FALSE);
gtk_window_set_default_size (GTK_WINDOW(window), 230, 100);
gtk_widget_set_size_request (window, 230, 100);
gtk_window_set_title (GTK_WINDOW(window), "config");
gtk_window_set_resizable (GTK_WINDOW(window), FALSE);
gtk_window_set_keep_above (GTK_WINDOW(window), TRUE);
center_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
gtk_container_add (GTK_CONTAINER (window), center_vbox);
combobox = gtk_combo_box_new ();
gtk_box_pack_start (GTK_BOX (center_vbox), combobox, FALSE, FALSE, 0);
scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 50, 1);
gtk_box_pack_start (GTK_BOX (center_vbox), scale, TRUE, TRUE, 0);
//focus-out-event event to auto hide window
gtk_widget_set_events (window, GDK_FOCUS_CHANGE_MASK);
g_signal_connect (G_OBJECT(window), "focus-out-event", G_CALLBACK(quit_function), NULL);
gtk_widget_show_all(window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
我想要这个窗口,这样当我单击任何子小部件(组合框/比例)时,它就不会关闭。但是,如果我单击窗口外的应用程序窗口应该退出。
答: 暂无答案
评论
linux
debian
ubuntu