c++ - 无法在 gtk 中使用 c++ 创建 appindicator。错误 : g_dbus_proxy_new: assertion 'G_IS_DBUS_CONNECTION (connection)' failed

标签 c++ linux ubuntu gtk

我尝试在 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.我怎样才能调试这个?

最佳答案

我添加了这个函数“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);
}
虽然程序不再显示错误,但我仍然不知道为什么会发生
希望有人能告诉我为什么,谢谢

关于c++ - 无法在 gtk 中使用 c++ 创建 appindicator。错误 : g_dbus_proxy_new: assertion 'G_IS_DBUS_CONNECTION (connection)' failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70664526/

相关文章:

android - 在 Ubuntu 11.10 上运行 adb 结果权限被拒绝

c++ - 在 QWebView 中覆盖页面回复

c++ - 是否可以使用 CRTP 访问父类主体内的成员?

python - 如何在整个目录中查找具有特定扩展名的文件,然后复制到另一台服务器

javascript - dayjs() 的时区错误

linux - 关闭 USB 端口的电源

c++ - 循环中的 RNG 并从字符串数组中打印单词

c++ - 如何将较新的 perf 工具前端与旧 perf 版本的记录一起使用

c++ - 使用 tcmalloc - 如何正确加载 malloc 扩展?

linux - 如何在 fstab 中将特定文件夹挂载到我的外部硬盘驱动器中?