0
votes

When I try to add a Gtk::box to a grid from another function upon button click I get the error:

gtk_grid_attach_next_to: assertion '_gtk_widget_get_parent (child) == NULL' failed

But if I copy the code from the add function(card_add) to the main function (kanban_init) It will be added to the grid without issue but I need to add a new box to the grid upon button press, which calls the card_add function to add the box to the grid.

In the header file (tabs.h):

Gtk::Grid kanbanGrid;
Gtk::Box todoBox, progressBox, doneBox, backlogBox;

Gtk::Box newCard;
Gtk::Label cardTitle, cardDesc

In the main function(tabs::kanban_init):

tAdd.signal_clicked().connect(sigc::mem_fun(*this, &tabs::card_add));

kanbanGrid.attach(todoBox, 1, 1);
kanbanGrid.attach(progressBox, 2, 1);
kanbanGrid.attach(doneBox, 3, 1);
kanbanGrid.attach(backlogBox, 4, 1);

In the add new box function (tabs::card_add):

newCard.set_size_request(300, 30);
newCard.pack_start(cardTitle, Gtk::PACK_SHRINK);
newCard.pack_start(cardDesc, Gtk::PACK_SHRINK);

cardTitle.set_label("title");
cardDesc.set_label("description");

kanbanGrid.attach_next_to(newCard, todoBox, Gtk::POS_BOTTOM);
1
What is tAdd? I would like to try to reproduce your issue.BobMorane

1 Answers

0
votes

I'm not 100% sure. Here my hypothesis:

  • _gtk_widget_get_parent (child) == NULL looks like a widget has already a parent (so already attached).
  • newCard should be allocated (Gtk::Manage), else the second time the callback is called kanbanGrid will attach to the same widget. Another solution if you have already attached, add logic to leave immediately the callback. But this does not explain why this error is triggered the 1st time.
  • Instead of attach_next_to() have you tried add() ?