我想将样式设置为组合框项目。试图使用CSS - 不工作。 所以我尝试通过g_object_set直接设置属性。 如果对象 gtk_list_store_new(1,G_TYPE_STRING); 一切正常并且应用了样式。但是如果 gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_STRING); 应用程序粉碎了g_object_set。
代码:
GError * error =0;
GtkComboBox* cb = GTK_COMBO_BOX(widget);
// GtkListStore *store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
GtkTreeIter iter;
gtk_list_store_insert_with_values(GTK_LIST_STORE(store), &iter, -1, 0, "Netherlands",1, "Netherlands", -1);
gtk_list_store_insert_with_values(GTK_LIST_STORE(store), &iter, -1, 0, "Japan",1, "Netherlands", -1);
gtk_combo_box_set_model(cb, GTK_LIST_STORE(store));
GtkCellRendererText* cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(cb), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(cb), cell, "text", 0, NULL);
GtkCellRendererText* cell2 = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(cb), cell2, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(cb), cell2, "text",1, NULL);
g_object_set(cell, "font", "Proxima Nova Rg 15");
g_object_set(cell, "foreground", "#a5adbe");
g_object_set(cell, "background", "#ffffff");
g_object_unref(store);
堆栈追踪:
strstr()
?? ()
g_object_set_valist()
g_object_set
代码在组合框的实现信号上执行
提前致谢!
答案 0 :(得分:0)
当我在docs中找到 cell-background 属性时,很明显我使用了错误的属性。但它不会单独工作,你必须指定 cell-background-set"我找到了here,我觉得
我的工作代码:
G_MODULE_EXPORT void on_countries_show(GtkWidget* widget, gpointer user_data)
{
GError * error =0;
GtkComboBox* cb = GTK_COMBO_BOX(widget);
GtkListStore *store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
GtkTreeIter iter;
int h = gtk_widget_get_allocated_height(widget);
h = 18;
GdkPixbuf *nl = gdk_pixbuf_new_from_file_at_scale ("flags/NL.png", h, h, TRUE, &error);
GdkPixbuf *jp = gdk_pixbuf_new_from_file_at_scale ("flags/JP.png", h, h, TRUE, &error);
gtk_list_store_insert_with_values(GTK_LIST_STORE(store), &iter, -1, 0, nl, 1, "Netherlands", -1);
gtk_list_store_insert_with_values(GTK_LIST_STORE(store), &iter, -1, 0, jp, 1, "Japan", -1);
gtk_combo_box_set_model(cb, GTK_LIST_STORE(store));
GtkCellRenderer* pb = gtk_cell_renderer_pixbuf_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(cb), pb, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(cb), pb, "pixbuf",0, NULL);
GtkCellRenderer* cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(cb), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(cb), cell, "text", 1, NULL);
g_object_set(pb,
"cell-background", "white",
"cell-background-set", TRUE,
NULL);
g_object_set(cell,
"font", "Proxima Nova Rg 15",
"background", "white",
"foreground", "#a5adbe",
"cell-background-set", TRUE,
NULL);
g_object_unref(store);
}
如果有人像我一样被困 - 欢迎你; - )