在 AppIndicator 中设置子菜单的操作

在 AppIndicator 中设置子菜单的操作

我正在尝试用 C 语言构建一个简单的指标。这是我的代码:

void make_indicator(){
    //Making the indicator
    appindicator = app_indicator_new("My Indicator", "/home/alex/Qt/normal_tray_icon/a.png", APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    //the construction of the main menu
    GtkWidget* indicatormenu = gtk_menu_new();
    //adding a submenu
    GtkWidget* submenu1 = gtk_menu_item_new_with_label("SubMenu1");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenu1), indicatormenu);

    /* HERE */
    //here comes the error! Adding an option to the submenu    
    GtkWidget* submenu1_option;
    submenu1_option = gtk_menu_item_new_with_label("Submenu option!");
    gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option);

    //adding an option to the main menu and connecting it to a slot
    GtkWidget* showapp_option;

    showapp_option = gtk_menu_item_new_with_label("Show App!");
    g_signal_connect(showapp_option, "activate", G_CALLBACK(show_app), this);
    gtk_menu_shell_append(GTK_MENU_SHELL(indicatormenu), showapp_option);

    //showing the indicator
    gtk_widget_show_all(indicatormenu);
    app_indicator_set_status(appindicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_attention_icon(appindicator, "dialog-warning");

    app_indicator_set_menu(appindicator, GTK_MENU (indicatormenu));
}

我无法将操作添加到子菜单。我将子菜单添加为简单菜单项,因此无法向其添加操作,可以吗?

我在编译时没有收到错误消息,但在运行时我收到:

(normal_tray_icon:9203): GLib-GObject-WARNING **: invalid cast from `GtkMenuItem' to `GtkMenuShell'

(normal_tray_icon:9203): Gtk-CRITICAL **: IA__gtk_menu_shell_insert: assertion `GTK_IS_MENU_SHELL (menu_shell)' failed

我能理解,但我不知道如何解决。

谢谢您的任何回答。

答案1

您仅为子菜单创建了项目,而不是子菜单。这可以按以下方式完成:

//添加子菜单:
//创建子菜单
GtkWidget *子菜单1 = gtk_menu_new();
GtkWidget *submenu1_item = gtk_menu_item_new_with_label("子菜单1");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenu1_item),子菜单1);

//将子菜单添加到主菜单
gtk_menu_shell_append(GTK_MENU_SHELL(indicatormenu),子菜单1_item);

//向子菜单添加一个选项    
GtkWidget* 子菜单1_选项;
submenu1_option = gtk_menu_item_new_with_label("子菜单选项!");
gtk_menu_shell_append(GTK_MENU_SHELL(子菜单1),子菜单1_option);

相关内容