强制GtkCellRenderer正确对齐列的最佳方法(或任何方法(是什么?我在gtk2.0的文档中找到了一篇关于它的文章——在这个链接下,PANGO_ALIGN_LEFT被提到为";对齐";财产,尽管它应该起作用,但它没有。通过类比,应该存在一个名为PANGO_ALIGN_RIGHT的常量,(如果有效(这应该可以解决我的问题。在搜索gtk源代码时,我发现0.0和1.0选项是";对齐";但这也没用。
(gtklistview_right:25878): GLib-GObject-WARNING **: 00:17:15.109: unable to set property 'alignment' of type 'PangoAlignment' from value of type 'gchararray'
代码:
gtklistview_right.c
#include <gtk/gtk.h>
enum {
LIST_ITEM = 0,
N_COLUMNS
};
void init_list(GtkWidget *list) {
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkListStore *store;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes("List Items", renderer,
"text", LIST_ITEM,
"alignment", PANGO_ALIGN_LEFT,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(list),
GTK_TREE_MODEL(store));
g_object_unref(store);
}
void add_to_list(GtkWidget *list, const gchar *str) {
GtkListStore *store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *list;
GtkWidget *vbox;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
list = gtk_tree_view_new();
gtk_window_set_title(GTK_WINDOW(window), "List view");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_default_size(GTK_WINDOW(window), 270, 250);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 5);
label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
gtk_container_add(GTK_CONTAINER(window), vbox);
init_list(list);
add_to_list(list, "Aliens");
add_to_list(list, "Leon");
add_to_list(list, "The Verdict");
add_to_list(list, "North Face");
add_to_list(list, "Der Untergang");
g_signal_connect(G_OBJECT (window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Makefile
CC=gcc
LIBS=`pkg-config --cflags --libs gtk+-2.0`
PROGRAMS=gtklistview_right
SRC=gtklistview_right.c
all: $(PROGRAMS)
%: %.c
$(CC) -o $@ $^ $(LIBS)
clean:
rm $(PROGRAMS)
您使用的是"alignment"属性,该属性用于对齐文本行,以防有多行文本。若要对齐所有文本,请使用"xalign"属性。
文件是https://developer.gnome.org/gtk3/stable/GtkCellRendererText.html#GtkCellRendererText--对准