我已经有了GTK +应用程序。有几个小部件:GtkWindow,GtkTreeView,GtkListStore。所有用户界面都是在Glade中创建的。
int main( int argc, char **argv )
{
GtkBuilder *builder;
GtkWidget *topWindow;
GtkTreeView *treeView;
GtkListStore *treeStore;
GError *error = NULL;
gtk_init( &argc, &argv );
builder = gtk_builder_new();
if( ! gtk_builder_add_from_file( builder, UI_FILE, &error ) )
{
g_warning( "%s", error->message );
g_free( error );
return( 1 );
}
topWindow = GTK_WIDGET(gtk_builder_get_object(builder, "topWindow"));
treeStore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststore"));
treeView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
entry1 = GTK_ENTRY(gtk_builder_get_object(builder, "entry1"));
entry2 = GTK_ENTRY(gtk_builder_get_object(builder, "entry2"));
entry3 = GTK_ENTRY(gtk_builder_get_object(builder, "entry3"));
gtk_builder_connect_signals (builder, NULL);
g_object_unref( G_OBJECT( builder ) );
gtk_widget_show( topWindow );
gtk_main();
return( 0 );
}
编译成功:
igor@igor-desktop:~/proj/TreeView_example$ make
gcc -Wall -g `pkg-config --cflags gtk+-2.0 gmodule-2.0` -c charter.c
gcc `pkg-config --libs gtk+-2.0 gmodule-2.0` charter.o -o charter
当我运行应用程序时,我收到一些警告:
(charter:3728): GLib-GObject-WARNING **: value "466" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "44" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "466" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "466" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "44" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "44" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "466" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "466" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "44" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
(charter:3728): GLib-GObject-WARNING **: value "44" of type `gint' is invalid or out of range for property `xalign' of type `gfloat'
这是Glade XML文件的一部分,其中包含GtkTreeView:
的描述 <object class="GtkTreeView" id="treeview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore</property>
<property name="search_column">2</property>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title">Имя</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="xalign">0</attribute>
<attribute name="sensitive">0</attribute>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn2">
<property name="title">Икс</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext2"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn3">
<property name="title">Игрек</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext3"/>
<attributes>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes"><b>TreeView</b></property>
<property name="use_markup">True</property>
</object>
</child>
</object>
你能解释我收到警告的原因吗?
答案 0 :(得分:4)
<attribute name="xalign">0</attribute>
<attribute name="sensitive">0</attribute>
<attribute name="text">0</attribute>
这表示您希望将单元格渲染器的 xalign ,敏感和 text 属性设置为列0的值
例如,如果模型中的行如下所示:
column0=A column1=B column2=C
xml中的那些行会将第一个单元格的 xalign ,敏感和文本设置为 A ,无论A值是多少,这很可能不是你想要的。
在Glade中,您可以通过右键单击树视图,选择编辑... ,然后转到层次结构选项卡来更改这些设置。您应该看到三列,每列都有一个单元格渲染器。
其余的取决于你想做什么。如果您不知道 xalign 和 sensitive 是什么,它们可能是偶然设置的,所以只需将它们更改回-1(这相当于从XML中删除相应的属性元素。 text 可能是正确的,所以请将其保留为0。
如果您想了解有关单元格渲染器属性的更多信息,可以从tree widget overview和TreeViewColumn.add_attribute documentation开始。不幸的是,这些并不能很清楚地解释这个概念你可能会在其他地方得到更好的解释。