2

我使用 GTK4 创建了一个简单的 C 应用程序,它有一个窗口、一个菜单栏、一个菜单和一个菜单项。我希望菜单读取“文件”,文件下方的菜单项读取“连接”。我如何编码菜单和菜单栏,以便我有一个菜单文件->连接?

#include <gtk/gtk.h>

static void connect(GApplication *app, gpointer *data) {
    puts("Connect menu item clicked.\n");
}

static void activate(GApplication *app, gpointer *data) {
    GtkWidget *win = gtk_application_window_new(GTK_APPLICATION(app));
    gtk_window_set_title(GTK_WINDOW(win), "IRC Client");
    gtk_window_set_default_size(GTK_WINDOW(win), 800, 800);

    GSimpleAction *act_connect = g_simple_action_new("Connect", NULL);
    g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(act_connect));
    g_signal_connect(act_connect, "activate", G_CALLBACK(connect), NULL);

    GMenu *menubar = g_menu_new();
    GMenuItem *menu_item_menu = g_menu_item_new("File", NULL);
    GMenu *menu = g_menu_new();
    GMenuItem *menu_item_connect = g_menu_item_new("Connect", "app.connect");
    g_menu_append_item(menu, menu_item_connect);
    g_object_unref(menu_item_connect);
    g_menu_item_set_submenu(menu_item_menu, G_MENU_MODEL(menu));
    g_menu_append_item(menubar, menu_item_menu);
    g_object_unref(menu_item_menu);

    gtk_application_set_menubar(GTK_APPLICATION(app), G_MENU_MODEL(menubar));
    gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(win), TRUE);
    gtk_window_present(GTK_WINDOW(win));
}

int main(int argc, char **argv) {
    GtkApplication *app;
    int stat;

    app = gtk_application_new("com.ircclient", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    stat = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return stat;
}
4

1 回答 1

-1

首先让我们从你的connect回调函数开始,它的原型应该是这样的:

static void connect ( GSimpleAction *simple_action, GVariant *parameter, gpointer *data 

在那之后,你有一个小问题,你很高兴。在这条线上:

GSimpleAction *act_connect = g_simple_action_new("Connect", NULL);

这是您的第一个参数,称为"Connect".

在这条线上:

GMenuItem *menu_item_connect = g_menu_item_new("Connect", "app.connect");

第二个参数是"app.connect",现在如果你比较"Connect""app.connect"会发现错误。

它区分大小写,只需替换"Connect"为就"connect"可以了。

现在对于您的问题,您需要另一个菜单,尝试理解以下演示:

#include <gtk/gtk.h>

static void action_clbk ( GSimpleAction *simple_action, G_GNUC_UNUSED GVariant *parameter, G_GNUC_UNUSED gpointer *data )
{
    g_print ( "The action %s was clicked.\n", g_action_get_name ( G_ACTION ( simple_action ) ) );
}

static void activate ( GApplication *app, G_GNUC_UNUSED gpointer *data )
{
    GtkWidget *win;
    GSimpleAction *act_connect;
    GSimpleAction *act_disconnect;

    /// ***
    GMenu *menu_bar;
    GMenu *network_menu;
    GMenu *server_menu;

    /// ***
    GMenuItem *menu_item_connect;
    GMenuItem *menu_item_disconnect;

    /// *** Menu Bar
    menu_bar = g_menu_new();

    /// *** Network_Menu
    network_menu = g_menu_new();
    g_menu_append_submenu ( menu_bar, "Network", G_MENU_MODEL ( network_menu ) );

    /// *** Server_Menu
    server_menu = g_menu_new();
    g_menu_append_submenu ( network_menu, "Server", G_MENU_MODEL ( server_menu ) );
    /// ***
    win = gtk_application_window_new ( GTK_APPLICATION ( app ) );
    gtk_window_set_title ( GTK_WINDOW ( win ), "IRC Client" );
    gtk_window_set_default_size ( GTK_WINDOW ( win ), 400, 400 );

    /// *** Create Connect and Disconnect Actions
    act_connect    = g_simple_action_new ( "connect", NULL );
    act_disconnect = g_simple_action_new ( "disconnect", NULL );

    /// *** Add them to the ActionMap
    g_action_map_add_action ( G_ACTION_MAP ( app ), G_ACTION ( act_connect ) );
    g_action_map_add_action ( G_ACTION_MAP ( app ), G_ACTION ( act_disconnect ) );

    /// *** Connect them to the activate Signal
    g_signal_connect ( act_connect,    "activate", G_CALLBACK ( action_clbk ), NULL );
    g_signal_connect ( act_disconnect, "activate", G_CALLBACK ( action_clbk ), NULL );

    /// *** Create the Connect Item
    menu_item_connect = g_menu_item_new ( "Connect", "app.connect" );
    g_menu_append_item ( server_menu, menu_item_connect );

    /// *** Create the Disconnect Item
    menu_item_disconnect = g_menu_item_new ( "Disconnect", "app.disconnect" );
    g_menu_append_item ( server_menu, menu_item_disconnect );

    /// ***
    gtk_application_set_menubar             ( GTK_APPLICATION ( app ), G_MENU_MODEL ( menu_bar ) );
    gtk_application_window_set_show_menubar ( GTK_APPLICATION_WINDOW ( win ), TRUE );

    /// ***
    gtk_window_present ( GTK_WINDOW ( win ) );

    /// *** Clean
    g_object_unref ( act_connect );
    g_object_unref ( act_disconnect );
    g_object_unref ( menu_item_connect );
    g_object_unref ( menu_item_disconnect );
    g_object_unref ( server_menu );
    g_object_unref ( network_menu );
    g_object_unref ( menu_bar );
}

int main ( int argc, char **argv )
{
    GtkApplication *app;
    int stat;

    /// ***
    app = gtk_application_new ( "com.ircclient", G_APPLICATION_FLAGS_NONE );
    g_signal_connect ( app, "activate", G_CALLBACK ( activate ), NULL );

    /// ***
    stat = g_application_run ( G_APPLICATION ( app ), argc, argv );
    g_object_unref ( app );

    /// ***
    return stat;
}

PS:

如果您需要了解更多关于GMenu或如何Actions工作的信息,请单击此处

于 2021-09-12T17:38:35.007 回答