如何在GtkTreeView中交替亮/暗行?

时间:2016-03-15 03:42:35

标签: css c gtk gtk3

我已阅读并尝试了现有的解决方案,我无法让它们发挥作用。我希望有人可以指出我做错了什么,或者告诉我为什么这些解决方案不再有效。

我想确定这些解决方案不起作用,所以我制作了这样的样式表:

GtkTreeView row {
    color: #FFFFFF;
    background-color: #FF0000;
}
GtkTreeView row:nth-child(even) {
    background-color: #FF00FF;
}
GtkTreeView row:nth-child(odd) {
    background-color: #00FFFF;
}
用鲜艳的色彩,只是为了使行颜色的差异真的很明显。然后,我做了一个小应用程序来加载树视图:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
  int i;
  gtk_init(&argc,&argv);
  //GtkBuilder* b = gtk_builder_new_from_file("derp.glade.xml");
  GtkWidget* top = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  GtkListStore* items = gtk_list_store_new(2,
                                           G_TYPE_STRING,
                                           G_TYPE_STRING);

  GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(items));
  gtk_tree_view_insert_column_with_attributes
    (GTK_TREE_VIEW(view),
     0,
     "Herp",
     gtk_cell_renderer_text_new(),
     "text",0,
     NULL);
  gtk_tree_view_insert_column_with_attributes
    (GTK_TREE_VIEW(view),
     1,
     "Derp",
     gtk_cell_renderer_text_new(),
     "text",1,
     NULL);
  gtk_container_add(GTK_CONTAINER(top),view);
  GtkCssProvider* prov = gtk_css_provider_new();
  gtk_css_provider_load_from_path
    (prov,
    "derp.css",
     NULL);
  gtk_style_context_add_provider
    (gtk_widget_get_style_context(view),
     GTK_STYLE_PROVIDER(prov),
     GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  GtkTreeIter iter;
  gtk_tree_model_get_iter_first(GTK_TREE_MODEL(items),&iter);

  for(i=0;i<3;++i) {
    gtk_list_store_insert_with_values
      (items,&iter,0,
       0, "Row",
       1, "Row",
       -1);
  }
  gtk_widget_show_all(top);
  gtk_main();
  return 0;
}

编译:

gcc -o test teststyle.c `pkg-config gtk+-3.0 --cflags --libs`

当我运行应用程序时,三行显示#00FFFF作为背景颜色。他们不会交替。他们只采用&#34;行的颜色:nth-​​child(odd)&#34;部分,甚至偶数行都采用那种颜色。搞乱css文件也可以做一些有趣的事情。切换奇数和偶数,例如:

GtkTreeView row {
    color: #FFFFFF;
    background-color: #FF0000;
}
GtkTreeView row:nth-child(odd) {
    background-color: #FF00FF;
}
GtkTreeView row:nth-child(even) {
    background-color: #00FFFF;
}

现在所有行都显示为#FF00FF,没有替换。我认为GTK完全没有以某种方式读取伪类,意外地转向&#34; GtkTreeView行:nth-​​child(odd)&#34;进入&#34; GtkTreeView行&#34;并完全错过了&#34; n-child(偶数)&#34;选择器完全。如果我删除奇数选择器并且只有偶数:

GtkTreeView row {
    color: #FFFFFF;
    background-color: #FF0000;
}
GtkTreeView row:nth-child(even) {
    background-color: #FF00FF;
}

现在它的背景颜色为#FF0000,因此完全无视(甚至)规则。

我尝试在树视图上设置规则提示,除了抱怨设置规则提示已被弃用之外,它没有做任何事情。我在基本的Arch系统上使用GTK 3.18.9,使用XFCE作为窗口管理器。我在这里做错了吗?或者是我的GTK版本搞砸了,不知何故?

2 个答案:

答案 0 :(得分:0)

我认为只有2个街区就足够了

GtkTreeView row:nth-child(odd) {
    background-color: #FF00FF;
}
GtkTreeView row:nth-child(even) {
    background-color: #00FFFF;
}

在python中的示例下面,在回答之前测试好了

#!/usr/bin/env python3
# -*- coding: ISO-8859-1 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk

window = Gtk.Window()
window.connect("destroy", lambda q: Gtk.main_quit())

liststore = Gtk.ListStore(str, int)
liststore.append(["Oranges", 5])
liststore.append(["Apples", 3])
liststore.append(["Bananas", 1])
liststore.append(["Tomatoes", 4])
liststore.append(["Cucumber", 1])
liststore.append(["potatoes", 10])
liststore.append(["apricot", 100])

treeview = Gtk.TreeView(model=liststore)
treeview.set_rules_hint( True )
window.add(treeview)

treeviewcolumn = Gtk.TreeViewColumn("Item")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)

treeviewcolumn = Gtk.TreeViewColumn("Quantity")
treeview.append_column(treeviewcolumn)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 1)
css_provider = Gtk.CssProvider()
css = """
                /* font operate on entire GtkTreeView not for selected row */
                GtkTreeView {font-weight: bold;font: Sans 20;font-style: italic;}
                GtkTreeView row:nth-child(even) {
                background-image: -gtk-gradient (linear,
                                                   left top,
                   left bottom,
                   from (#d0e4f7),
                   color-stop (0.5, darker (#d0e4f7)),
                   to (#fdffff));
                }
                GtkTreeView row:nth-child(odd) {
                background-image: -gtk-gradient (linear,
                                                   left top,
                   left bottom,
                   from (yellow),
                   color-stop (0.5, darker (yellow)),
                   to (#fdffff));
                }
                /* next line only border action operate */
                GtkTreeView:selected{color: white; background: green; border-width: 1px; border-color: black;}
                /* next line for Gtk.TreeViewColumn */
                column-header .button{color: white; background: purple;}

            """
css_provider.load_from_data(css)
screen = Gdk.Screen.get_default()
style_context = window.get_style_context()
style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

window.show_all()

Gtk.main()

答案 1 :(得分:0)

这可能是一个错误。在this example中,row:nth-child应用于不同的GtkTreeView。