我已经有一个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>
您能解释我为什么收到警告吗?
最佳答案
<attribute name="xalign">0</attribute>
<attribute name="sensitive">0</attribute>
<attribute name="text">0</attribute>
这表示您希望将单元格渲染器的xalign,sensitive和text属性设置为列0的值。
例如,如果模型中的一行如下所示:
column0=A column1=B column2=C
xml中的这些行会将第一个单元格的xalign,敏感和文本设置为A,无论A值是多少,这很可能不是您想要的。
在Glade中,可以通过右键单击树 View ,选择“编辑...”,然后转到“层次结构”选项卡来更改这些设置。您应该看到三列,每个列具有一个单元格渲染器。
其余的取决于您想做什么。如果您不知道xalign和sensitive是什么,可能是偶然设置的,因此只需将它们更改回-1(这相当于从XML中删除相应的属性元素)。文本可能是正确的,因此将其保留为0。
如果要了解有关单元格渲染器属性的更多信息,则可以从tree widget overview和TreeViewColumn.add_attribute documentation开始。不幸的是,这些并不能很清楚地解释这个概念。您可能会在其他地方得到更好的解释。
关于c++ - 在GTK +中使用GtkTreeView会遇到一些问题[警告],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4908990/