C和GTK3 - 使用Glade时不显示应用程序菜单

时间:2017-05-12 20:46:09

标签: c gtk gtk3 glade

使用Glade(GtkBuilder)时,我无法获得正确的应用程序菜单。这是我目前使用的代码:

main.c中:

void app_init(GtkApplication* app, void* userdata)
{
    (void)userdata;
    GActionEntry appactions[] = {
        {"about", app_aboutaction, NULL, NULL, NULL, {0}},
        {"quit", app_quitaction, NULL, NULL, NULL, {0}},
    };

    g_action_map_add_action_entries(
        G_ACTION_MAP(app),
        appactions,
        sizeof appactions / sizeof *appactions,
        app
    );

    GMenu* menu = g_menu_new();
    g_menu_append(menu, "About", "app.about");
    g_menu_append(menu, "Quit", "app.quit");

    gtk_application_set_app_menu(app, G_MENU_MODEL(menu));
}

void app_ctor(GtkApplication* app, void* userdata)
{
    (void)userdata;

    GtkBuilder* builder = gtk_builder_new_from_file("./builder.ui");
    GtkWindow* appwin = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
    gtk_window_set_application(appwin, app);
}

int main(int argc, char* argv[])
{
    GtkApplication* app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "startup", G_CALLBACK(app_init), NULL);
    g_signal_connect(app, "activate", G_CALLBACK(app_ctor), NULL);

    return g_application_run(G_APPLICATION(app), argc, argv);
}

builder.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkApplicationWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

生成的应用程序菜单:

result

预期的应用程序菜单:

expected result

我认为它与我没有调用应用程序窗口构造​​函数有关,因为它是使用GtkBuilder或类似的东西创建的。

我能做什么的想法?

1 个答案:

答案 0 :(得分:5)

问题在于ui构建器文件,其中visible标志设置为true:

  

(应用程序)窗口必须分配GtkApplication实例   之前显示

因此,将builder.ui Visible属性设置为False:

<property name="visible">False</property>

由于窗口未显示,您必须在设置应用程序后手动执行此操作,例如gtk_window_present:

void app_ctor(GtkApplication* app, G_GNUC_UNUSED gpointer user_data)
{
    GtkBuilder* builder = gtk_builder_new_from_file("./builder.ui");
    GtkWindow* appwin = GTK_WINDOW(gtk_builder_get_object(builder, "window"));
    gtk_window_set_application(appwin, app);
    gtk_window_present(appwin);
}