0

我已经包含了一个简短的代码片段。我正在制作一个有 5 列的树视图。我已将最后一列粘贴到 for 循环中,该循环生成要存储在 GtkListStore 中的数据。我可以轻松地将树视图的列居中。我在网上查看了如何将 GtkListStore 中的文本居中,但我没有找到解决方案。我浏览了文档:

http://developer.gnome.org/gtk3/3.4/GtkListStore.html

在最后一个链接上,我没有看到对齐属性。有没有办法通过 GtkTreeModel 对齐所有对象?我还没有找到任何使用 GtkListStore 和对齐文本的在线示例......非常感谢您的帮助!

// Append Table Velocity column
column = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(column, "Pressure");
gtk_tree_view_column_set_min_width(column, 60);
gtk_tree_view_column_set_alignment(column, 0.5); // 0.0 left, 0.5 center, 1.0 right
// Code Above center's the column title in the Tree View

renderer = gtk_cell_renderer_text_new();
g_object_set( G_OBJECT( renderer ), "xalign", 0.5 );    // xalign, 0.5
// Code above doesn't change alignment..

gtk_tree_view_column_pack_start(column, renderer, FALSE);
gtk_tree_view_column_set_attributes(column, renderer, "text", 4, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

// List Store
liststore = gtk_list_store_new(5, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore));

int i;
for(i=1; i<=6; i++) {
// Append test data
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, 0, i, 1, "5", 2, "0.2", 3, "123", 4, "0.5", -1);
// How do I center the text stored in the GtkListStore?
}

gtk_widget_show_all(window);
4

2 回答 2

2

我能够找到一个名为 gtk_tree_view_column_with_attributes() 的便捷函数。使用这个函数和另外两行代码,我可以方便地将标题和列表存储中的文本居中。

// Append Pressure column
column = gtk_tree_view_column_new_with_attributes("Pressure", renderer, "text", 4, NULL);
gtk_tree_view_column_set_alignment(column, 0.5);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
于 2013-02-18T19:58:01.440 回答
1

Try g_object_set (renderer, "xalign", 0.5, NULL); with the NULL at the end, g_object_set needs a sentinel. Actually leaving out the sentinel should have given you at least a compiler warning, or did you leave it out deliberately or by mistake?

于 2013-02-06T20:52:00.560 回答