如何在GtkTreeView中交替显示浅色/深色行?

4
我已经阅读并尝试过现有的解决方案,但是它们都不能正常工作。我希望有人能指出我做错了什么,或者告诉我为什么这些解决方案不再起作用。
以下是那些解决方案: 我想确保这些解决方案无法工作,所以我制作了这样一个样式表:
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背景色。它们不是交替的。它们只从“row:nth-child(odd)”部分获取颜色,即使偶数行也采用该颜色。更改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在某种情况下完全无法读取伪类,偶然地将“GtkTreeView row:nth-child(odd)”转换为“GtkTreeView row”,而完全忽略了“nth-child(even)”选择器。如果我删除奇数选择器,只剩下偶数选择器:
GtkTreeView row {
    color: #FFFFFF;
    background-color: #FF0000;
}
GtkTreeView row:nth-child(even) {
    background-color: #FF00FF;
}

现在它的背景颜色是#FF0000,所以它完全忽略了规则。我曾尝试在树形视图中设置规则提示,但除了抱怨规则提示已被弃用外,什么也没做。我使用的是3.18.9版本的GTK,在基本的Arch系统上,窗口管理器是XFCE。我这里有什么问题吗?还是我的GTK版本出了问题?
2个回答

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()

代码示例无法运行。"Traceback (most recent call last): File "zebragtk.py", line 58, in <module> css_provider.load_from_data(css) TypeError: Item 0: Must be number, not str" - Sam
我有点惊讶。我的gtk版本是GtK 3.8.10,而Python版本是2.7.6。代码可以正常运行。我不知道是什么原因。有人能解释一下吗? - Manmax
我正在使用GTK 3.18.2版本。听起来很熟悉,set_rules_hint在GTK 3.14中已被弃用。 https://developer.gnome.org/gtk3/3.16/GtkTreeView.html#gtk-tree-view-set-rules-hint - Sam
1
花了一些时间用C语言实现了这个:http://pastebin.com/wRGBxHYB,使用```gcc -o derpp pkg-config --cflags --libs gtk+-3.0 derp.c```编译,但它仍然忽略了(even)选择器。我有GTK 3.18.9。不知道为什么Python不起作用,但我怀疑是过度分层的抽象接口所致。 - cyisfor
在我的GTK#项目中也是一样的情况。这些行不会随着上面的CSS代码交替。 - Q''
显示剩余2条评论

0

这很可能是一个bug。在这个例子中,row:nth-child被应用于不同的GtkTreeView。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接