简体   繁体   English

无法在 gtk 中使用 c++ 创建 appindicator。 错误:g_dbus_proxy_new:断言“G_IS_DBUS_CONNECTION(连接)”失败

[英]Unable to create appindicator using c++ in gtk. error : g_dbus_proxy_new: assertion 'G_IS_DBUS_CONNECTION (connection)' failed

I try to create indicator on Ubuntu.我尝试在 Ubuntu 上创建指标。 The following code works on Ubuntu 18, but not on Ubuntu 20.以下代码适用于 Ubuntu 18,但不适用于 Ubuntu 20。

void TestApplication(GtkApplication* app, gpointer user_data) {

    /* This function just keeps indicator exist. */
    gtk_application_window_new (app);

    GtkWidget* menu = gtk_menu_new();
    GtkWidget* menu_item = gtk_menu_item_new_with_label("menu_item");
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);

    AppIndicator *indicator = app_indicator_new("test_indicator", icon_path, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_menu(indicator, GTK_MENU(menu));

    gtk_widget_show_all(menu);
}


int main(int argc, char *argv[]) {

    GtkApplication *app = gtk_application_new ("org.gtk.test", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (TestApplication), NULL);
    g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
    return 0;
}

Always showing error on Ubuntu 20: Ubuntu 20 上始终显示错误:

GLib-GIO-CRITICAL **: 17:08:47.905: g_dbus_proxy_new: assertion 'G_IS_DBUS_CONNECTION (connection)' failed GLib-GIO-CRITICAL **:17:08:47.905:g_dbus_proxy_new:断言“G_IS_DBUS_CONNECTION(连接)”失败

Sometimes no errors are displayed and it runs successfully on Ubuntu 20.有时不显示错误,并且在 Ubuntu 20 上成功运行。

If I modify the code to the following, still the same error message is displayed.如果我将代码修改为以下内容,仍然会显示相同的错误消息。

void TestApplication(GtkApplication* app, gpointer user_data) {

    /* This function just keeps indicator exist. */
    gtk_application_window_new (app);

    app_indicator_new("test_indicator", icon_path, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

}

If the program runs only "gtk_application_window_new" or "app_indicator_new" no error will be displayed.如果程序仅运行“gtk_application_window_new”或“app_indicator_new”,则不会显示错误。

My question is:我的问题是:

1.How can I solve this error? 1.我该如何解决这个错误?

2.How can I debug this? 2.我该如何调试?

I added this function "gtk_widget_show(window);"我添加了这个 function "gtk_widget_show(window);" then it works fine.然后它工作正常。

void TestApplication(GtkApplication* app, gpointer user_data) {

    /* This function just keeps indicator exist. */
    GtkWidget* window = gtk_application_window_new (app);
    gtk_widget_show(window);

    GtkWidget* menu = gtk_menu_new();
    GtkWidget* menu_item = gtk_menu_item_new_with_label("menu_item");
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);

    AppIndicator *indicator = app_indicator_new("test_indicator", icon_path, APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_menu(indicator, GTK_MENU(menu));

    gtk_widget_show_all(menu);
}

Although the program doesn't show the error anymore, but I still don't know why it happened Hope someone can tell me why, thanks虽然程序不再显示错误,但我仍然不知道为什么会发生希望有人能告诉我为什么,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM