Reputation: 2320
I want add dynamically new Gtk::ListBoxRow(s) to a Gtk::ListBox, but they don't show up at all. During testing I noticed, that even a simple function isn't able to add a new Gtk::ListBoxRow.
#include <gtkmm.h>
#include <iostream>
using namespace std;
Gtk::ListBox* listbox;
void test() {
Gtk::Label other("other");
Gtk::Box otherbox;
otherbox.pack_start(other);
Gtk::ListBoxRow otherrow;
otherrow.add(otherbox);
listbox->append(otherrow);
// this doesn't help either
listbox->show_all_children();
}
int main(int argc, char* argv[]) {
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "im.lost");
Gtk::ApplicationWindow window;
window.set_title("I'm lost");
window.set_position(Gtk::WIN_POS_CENTER);
window.set_default_size(600, 400);
window.set_border_width(10);
listbox = new Gtk::ListBox();
listbox->set_selection_mode(Gtk::SELECTION_NONE);
Gtk::Label foo("foo");
Gtk::Label fooo("fooo");
Gtk::Box foobox;
foobox.pack_start(foo);
foobox.pack_start(fooo);
Gtk::ListBoxRow foorow;
foorow.add(foobox);
listbox->append(foorow);
Gtk::Label bar("bar");
Gtk::Label barr("barr");
Gtk::Box barbox;
barbox.pack_start(bar);
barbox.pack_start(barr);
Gtk::ListBoxRow barrow;
barrow.add(barbox);
listbox->append(barrow);
Gtk::Label baz("baz");
Gtk::Label bazz("bazz");
Gtk::Box bazbox;
bazbox.pack_start(baz);
bazbox.pack_start(bazz);
Gtk::ListBoxRow bazrow;
bazrow.add(bazbox);
listbox->append(bazrow);
test();
window.add(*listbox);
window.show_all_children();
return app->run(window);
}
This code can be compiled and run with:
g++ -o listbox listbox.cpp `pkg-config gtkmm-3.0 --cflags --libs` && ./listbox
What I'm doing wrong?
Thank you
Upvotes: 0
Views: 679
Reputation:
In your function test()
, the widgets that you created are destroyed when they go out of scope at the exit of the function. Therefore they are not available to be shown. What you need to do is create the widgets with new
and have them managed by the parent widget. The gtkmm book describes more on widget management. https://developer.gnome.org/gtkmm-tutorial/stable/sec-memory-widgets.html.en
This is a corrected version of your function test()
.
void test() {
Gtk::Label *other = Gtk::manage(new Gtk::Label("other"));
Gtk::Box *otherbox = Gtk::manage(new Gtk::Box());
otherbox->pack_start(*other);
Gtk::ListBoxRow *otherrow = Gtk::manage(new Gtk::ListBoxRow());
otherrow->add(*otherbox);
listbox->append(*otherrow);
}
Upvotes: 1