gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:40 24 4
gpt4 key购买 nike

我正在使用这段代码创建一个带有彩色背景/文本的组合框:

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

我的问题是,如何设置列表存储或组合框的背景,使图片中没有空白?谢谢!

最佳答案

我使用的是 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 ,它是实现此类目的的便捷工具。

关于c++ - 如何在 GTK2 中设置 GTKListStore/GTKComboBox 的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com