我用树店作为模型进行了树视图。窗口按预期显示,但当我点击“+”展开项目时,我收到此消息:
GLib-CRITICAL **: Source ID 221 was not found when attempting to remove it
这是我的代码:
#include <gtk/gtk.h>
/* compile with: */
/* gcc main.c -o boxy `pkg-config --cflags --libs gtk+-2.0` */
typedef struct {
GtkWidget *toplevel;
GtkWidget *treeview;
} Widgets;
enum { ITEM_PARENT, ITEM_CHILD };
typedef struct {
gint tipo;
gint id;
gchar *nombre;
gint cantidad;
} Lista;
void addColumn (GtkTreeView *tv, const gchar* title, gint pos) {
GtkCellRenderer *tmp;
tmp = gtk_cell_renderer_text_new ();
g_object_set (tmp, "editable", TRUE, "editable-set", TRUE, NULL);
gtk_tree_view_insert_column_with_attributes (tv, -1, title, tmp, "text", pos, NULL);
}
void setupTree (GtkTreeView *tv) {
const Lista lista[] = {
{ITEM_PARENT, 125, "Superman", 2},
{ITEM_CHILD, 23, "Batman", 1},
{ITEM_CHILD, 7, "Hulk", 5},
{ITEM_PARENT, 65, "Iron Man", 2},
{-1, -1, NULL, -1}
};
GtkTreeStore *model;
GtkTreeIter last;
gint pos;
model = gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT);
addColumn (tv, "ID", 0);
addColumn (tv, "Nombre", 1);
addColumn (tv, "Cantidad", 2);
for (pos = 0; lista[pos].tipo != -1; pos++) {
GtkTreeIter iter;
if (lista[pos].tipo == ITEM_PARENT) {
gtk_tree_store_append (model, &iter, NULL);
last = iter;
} else if (lista[pos].tipo == ITEM_CHILD) {
gtk_tree_store_append (model, &iter, &last);
}
gtk_tree_store_set (model, &iter, 0, lista[pos].id, 1, lista[pos].nombre, 2, lista[pos].cantidad, -1);
}
gtk_tree_view_set_model (tv, GTK_TREE_MODEL (model));
g_object_unref (model);
}
int main (int argc, char *argv[]) {
Widgets *ptr;
GtkWidget *scroll;
gtk_init (&argc, &argv);
ptr = g_slice_new0(Widgets);
ptr->toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_OUT);
ptr->treeview = gtk_tree_view_new ();
setupTree (GTK_TREE_VIEW (ptr->treeview));
g_signal_connect (ptr->toplevel, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (ptr->toplevel), 10);
gtk_container_add (GTK_CONTAINER (scroll), ptr->treeview);
gtk_container_add (GTK_CONTAINER (ptr->toplevel), scroll);
gtk_widget_show_all (ptr->toplevel);
gtk_main ();
g_slice_free (Widgets, ptr);
return 0;
}
有什么想法吗?
答案 0 :(得分:46)
这不是代码中的错误,也不是崩溃。它实际上只是一个警告,g_source_remove()
被调用来断开已经断开的某个事件处理程序,在这种情况下,是在gtk的一部分代码中。
警告本身是在this commit中的glib 2.39中引入的,似乎只有主要Linux用户受到影响,因为其他发行版还没有更新。
在大多数情况下,这是完全无害的,只是一种烦恼。如果它源于您自己的代码中的g_source_remove()
调用,那么可能值得一看。
在g_log上设置断点以查找导致它的原因:
(gdb) break g_log
Breakpoint 1 at 0x7ffff5ea5a70
(gdb) c
Continuing.
Breakpoint 1, 0x00007ffff5ea5a70 in g_log () from /usr/lib/libglib-2.0.so.0
(gdb) bt
#0 0x00007ffff5ea5a70 in g_log () from /usr/lib/libglib-2.0.so.0
#1 0x00007ffff5e9d9dc in g_source_remove () from /usr/lib/libglib-2.0.so.0
#2 0x00007ffff79bd0f5 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#3 0x00007ffff79cc1a4 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#4 0x00007ffff79cda66 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#5 0x00007ffff78d7435 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#6 0x00007ffff616e3d8 in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#7 0x00007ffff617fb1b in ?? () from /usr/lib/libgobject-2.0.so.0
#8 0x00007ffff6187719 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#9 0x00007ffff6187d02 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#10 0x00007ffff79e6fe4 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#11 0x00007ffff78d5be4 in gtk_propagate_event () from /usr/lib/libgtk-x11-2.0.so.0
#12 0x00007ffff78d5f9b in gtk_main_do_event () from /usr/lib/libgtk-x11-2.0.so.0
#13 0x00007ffff75519cc in ?? () from /usr/lib/libgdk-x11-2.0.so.0
#14 0x00007ffff5e9eb84 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#15 0x00007ffff5e9edc8 in ?? () from /usr/lib/libglib-2.0.so.0
#16 0x00007ffff5e9f08a in g_main_loop_run () from /usr/lib/libglib-2.0.so.0
#17 0x00007ffff78d5087 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0
#18 0x000000000040152f in main ()
回溯从gtk_main一直到g_log,而不会在代码中传递一次,所以这可能会影响任何带有树视图的gtk程序。