6

I'm trying to use the set_cell_data_func on a Gtk.TreeViewColumn. My code compiles but gives a segmentation error when running. I have also tried the insert_column_with_data_func instead but the result is the same.

Here is my code, i hope you'll be able to help me :)

public class Application : Gtk.Window {
    public Application () {
        this.destroy.connect (Gtk.main_quit);
        this.set_default_size (600, 400);

        Gtk.ListStore store = new Gtk.ListStore (2, typeof(int),
            typeof(string));
        Gtk.TreeIter iter;

        store.append (out iter);
        store.set (iter, 0, 0, 1, "A");
        store.append (out iter);
        store.set (iter, 0, 1, 1, "B");
        store.append (out iter);
        store.set (iter, 0, 0, 1, "C");
        store.append (out iter);
        store.set (iter, 0, 0, 1, "D");

        Gtk.TreeView view = new Gtk.TreeView.with_model (store);
        this.add(view);

        Gtk.CellRendererText cell = new Gtk.CellRendererText ();
        Gtk.TreeViewColumn col = new Gtk.TreeViewColumn.with_attributes (
            "Value", cell, "text", 1);
        col.set_cell_data_func (cell, (Gtk.CellLayoutDataFunc)render_value);
        view.append_column (col);
    }

    public void render_value (Gtk.TreeViewColumn column, Gtk.CellRendererText
        cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
        var val = Value (typeof(int));
        model.get_value (iter, 0, out val);
        if ((int)val==1) (cell as Gtk.CellRendererText).background = "#b8cb04";
        val.unset ();
    }

    public static int main (string[] args) {
        Gtk.init (ref args);
        Application app = new Application ();
        app.show_all ();
        Gtk.main ();
        return 0;
    }
}
4

1 回答 1

5

在调试翻译后的 c 源代码后,我发现了这个错误。

vala翻译

public void render_value                            (Gtk.TreeViewColumn column, 
                                                     Gtk.CellRendererText cell, 
                                                     Gtk.TreeModel model, 
                                                     Gtk.TreeIter iter)

从您的代码到以下 c 等效项

void application_render_value                       (Application* self,
                                                     GtkTreeViewColumn* column,
                                                     GtkCellRendererText* cell,
                                                     GtkTreeModel* model, 
                                                     GtkTreeIter* iter)

我将此与参考文档进行了比较。

数据函数的签名定义如下

void                (*GtkCellLayoutDataFunc)        (GtkCellLayout *cell_layout,
                                                     GtkCellRenderer *cell,
                                                     GtkTreeModel *tree_model,
                                                     GtkTreeIter *iter,
                                                     gpointer data);    

关于您已实现的方法,这意味着参数向右移动了一位。所以以下适用于您的数据函数/方法

  • column实际上是单元格渲染器
  • cell是树模型
  • model是迭代器并且
  • iter是传递给函数/方法的数据

如果您将数据函数/方法的声明更改为

public void render_value (Gtk.CellRendererText cell, 
                          Gtk.TreeModel model, Gtk.TreeIter iter, Object data)

一切都应该正常。

也许这是因为它CellLayoutDataFunc被定义为public delegate void。但由于我对此一无所知vala,这只是一个猜测。如果不是这种情况,您可以将其发布在相关的vala邮件列表中。

于 2013-08-06T16:39:24.057 回答