为什么只有当我改变窗口(焦点)时,GtkInfoBar才可见?
按F5键使信息栏可见关闭信息栏并再次按F5键。
在我的Debian8(GNOME3.20)系统中,信息栏只在第一次可见,从第二次起,只有当我改变应用程序窗口的焦点时,它才可见。

// gcc `pkg-config --cflags gtk+-3.0` -o info_bar infobar_nonglade.c `pkg-config --libs gtk+-3.0`
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

// Callback to close the window with Esc key
static gboolean check_escape(GtkWidget *widget, GdkEventKey *event, gpointer data) {
        if (event->keyval == GDK_KEY_Escape) {
                gtk_main_quit();
                return TRUE;
        }
        return FALSE;
}

static gboolean on_key_f5(GtkWidget *widget, GdkEventKey *event, gpointer infobar) {
        if (event->keyval == GDK_KEY_F5) {
                gtk_widget_set_visible(infobar, !(gtk_widget_get_visible(infobar)));
                return TRUE;
        }
        return FALSE;
}

int main (int argc, char **argv) {
        gtk_init(&argc, &argv);
        GtkWidget *button, *content_area, *infobar, *message_label, *vbox, *window;
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
        infobar = gtk_info_bar_new();
        button = gtk_button_new_with_label ("OK");
        gtk_window_set_default_size (GTK_WINDOW (window), 1024, 600);
        gtk_widget_set_no_show_all (infobar, TRUE);
        gtk_info_bar_add_button (GTK_INFO_BAR (infobar),
                                 "OK",
                                 GTK_RESPONSE_OK);
        g_signal_connect (infobar, "response", G_CALLBACK (gtk_widget_hide), NULL);
        gtk_container_add (GTK_CONTAINER (window), vbox);
        gtk_box_pack_start (GTK_BOX (vbox), infobar, FALSE, FALSE, 0);
        gtk_widget_show_all (vbox);
        // Connect callback to close the window with the Esc key
        g_signal_connect (window, "key_press_event", G_CALLBACK (check_escape), NULL);
        g_signal_connect (window, "key_press_event", G_CALLBACK (on_key_f5), infobar);
        gtk_widget_show_all (window);
        gtk_main();

        return 0;
}

最佳答案

我不能在我的机器上测试,但你可以尝试:

gtk_widget_set_visible(infobar, !(gtk_widget_get_visible(infobar)));

而不是
gtk_widget_set_no_show_all (infobar, FALSE);
gtk_widget_show_all (infobar);

另外,为什么要使返回TRUE的事件失效?

10-06 05:07