Changing font color on a column in treeview gtk

一笑奈何 提交于 2019-12-13 04:25:12

问题


I've a treeview and I want to change the text color on one column.

How could I do this??

thanks


回答1:


(Refering to the standard C methods, haven't done much with Vala so far)

There are several ways to achieve this. You can change the settings of the text cellrenderer (GtkCellRendererText), an example would be

g_object_set (your_text_cell_renderer, "foreground", 
              "red", "foreground-set", TRUE);

Another way is using markup:

highlighted_txt = g_strconcat ("<span background='yellow' foreground='black'>", 
                               my_text, "</span>", NULL);
g_object_set (your_text_cell_renderer, "markup", highlighted_txt, NULL);

To change the font color of each column individually on certain conditions, gtk_tree_view_insert_column_with_data_func or gtk_tree_view_column_set_cell_data_func are used, they are described in the online documentation for GtkTreeView. You can use different text cell renderers for each column to keep settings separate.

It can be done in a way similar like this:

for (columns_cnt = 0; columns_cnt < NUMBER_OF_COLUMNS; columns_cnt++) {
  text_renderer = gtk_cell_renderer_text_new ();
  columns[columns_cnt] = gtk_tree_view_column_new_with_attributes
                         (column_header_txt[columns_cnt], text_renderer, 
                         "text", columns_cnt, NULL);
  gtk_tree_view_column_set_cell_data_func (columns[columns_cnt], text_renderer, 
                                           (GtkTreeCellDataFunc) 
                                           set_column_attributes, NULL, NULL);
}

...

static void set_column_attributes (GtkTreeViewColumn *cell_column, 
                                   GtkCellRenderer   *txt_renderer,
                                   GtkTreeModel      *cell_model, 
                                   GtkTreeIter       *cell_iter, 
                                   gpointer          pointer)
{
 // Use g_object_set or something else here.
}



回答2:


First, you have to set up a column in your model describing the color for each row. Then, you use gtk_tree_view_insert_column_with_attributes, to set the foreground attribute to the position of the color column.



来源:https://stackoverflow.com/questions/14006658/changing-font-color-on-a-column-in-treeview-gtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!