我创建了一个带有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;
}
发现问题。我应该为 Gtk 应用程序设置窗口。所需的其他代码:
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 filen",
"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));