如何在GTK2中设置GTKListStore / GTKComboBox的背景?

时间:2017-06-05 15:14:52

标签: c++ combobox gtk background-color gtk2

我使用此代码创建带有彩色背景/文字的组合框:

GtkListStore *liststore;
GtkWidget *combo;
GtkCellRenderer *column;
liststore = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
for(int i=0; i<10; i++) {
    gtk_list_store_insert_with_values(liststore, NULL, -1, 0, "Default", 1, "white", 2, "black", -1);
}
combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(liststore));
g_object_unref(liststore);
column = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), column, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), column, "text", 0, "foreground", 1, "background", 2, NULL);

它有效。它看起来像这样: enter image description here

我的问题是,如何设置列表库或组合框的背景,以便图片中没有空格? 谢谢!

1 个答案:

答案 0 :(得分:2)

我正在使用Numix主题,所以“边框”是红色的。您可以使用css覆盖主题样式:

GtkCssProvider *provider;

provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "menuitem { background: #000; } menuitem:hover { background: #FFF; } .combo { background: #000; }", -1, NULL);
gtk_style_context_add_provider (
  GTK_STYLE_CONTEXT (gtk_widget_get_style_context (GTK_WIDGET (combo))),
  GTK_STYLE_PROVIDER (provider),
  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (combo),
                                           GTK_STYLE_PROVIDER (provider),
                                           GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref (provider);

结果如下: screenshot

这是完整的源代码: https://pastebin.com/wDeUpb8A

另请查看GtkInspector,这是一个方便的工具。