繁体   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

我尝试在 Ubuntu 上创建指标。 以下代码适用于 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;
}

Ubuntu 20 上始终显示错误:

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

有时不显示错误,并且在 Ubuntu 20 上成功运行。

如果我将代码修改为以下内容,仍然会显示相同的错误消息。

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);

}

如果程序仅运行“gtk_application_window_new”或“app_indicator_new”,则不会显示错误。

我的问题是:

1.我该如何解决这个错误?

2.我该如何调试?

我添加了这个 function "gtk_widget_show(window);" 然后它工作正常。

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);
}

虽然程序不再显示错误,但我仍然不知道为什么会发生希望有人能告诉我为什么,谢谢

暂无
暂无

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

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