2

我用 glade 创建了一个 UI。

当我运行程序时,它没有给出任何错误,但也没有显示主窗口。它只是退出。

我的完整 UI 文件位于https://paste.gnome.org/pfxz888er 完整的 C 程序位于https://paste.gnome.org/p7rxhhbk2

请告诉我我的代码是否有问题。重要的部分是:

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

static void create (GtkApplication *app, gpointer user_data)
{
GtkBuilder *main_interface;
main_interface = gtk_builder_new_from_file ("/home/aj/learning/front.glade");

//  gtk_builder_connect_signals (main_interface,NULL);
gtk_builder_add_callback_symbol (main_interface,
                             "cb_about",
                             G_CALLBACK(cb_about));

gtk_builder_add_callback_symbol (main_interface,
                             "cb_enc_mode",
                             G_CALLBACK(cb_enc_mode));
..... all other callbacks........

GObject *top_window;
top_window = gtk_builder_get_object (main_interface,
                    "top_window");
gtk_widget_show_all (GTK_WIDGET(top_window));
}

int main (int argc, char *argv[])
{
int status;
GtkApplication *app;
app = gtk_application_new ("org.aj.gui",G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate",G_CALLBACK(create),NULL);
status = g_application_run (G_APPLICATION(app),argc,argv);
g_object_unref (app);
return status;
}
4

1 回答 1

3

发现问题。我应该为 GtkApplication 设置窗口。需要额外的代码:

GtkWidget *top_window;
top_window = GTK_WIDGET (gtk_builder_get_object (main_interface,"top_window"));
if (!top_window) {
    g_critical ("Widget \"%s\" is missing in UI file\n",
            "top_window");
}
g_object_unref (main_interface);
gtk_window_set_application (GTK_WINDOW (top_window), GTK_APPLICATION (app));
gtk_widget_show_all (GTK_WIDGET(top_window));
于 2016-01-17T14:12:44.613 回答