[GTK3] Adding widget to container got from a builder

Hello everyone,

sorry for the probably pretty easy question, but after a couple hours I still have no idea what the problem.

I have a UI file made in Glade, inside said UI file somewhere there is a Box with an id. Inside said box I need to add and remove widgets dynamically (it will end up being an image previewer).

I managed to make this work in Python by doing the following (this was a prototype)

window = builder.get_object("window1")
pictureFrame = builder.get_object("PreviewBox")
pictureFrame.pack_start(PictureView(scanner.collectionPictures[0]), True, True, 0)
window.show_all()

Now I’m trying to make the full application in Cpp, but when doing the following:

Gtk::Box* previewBox;
insertData_Builder->get_widget<Gtk::Box>("PreviewBox", previewBox);
Gtk::Label label = Gtk::Label("test label");
previewBox->pack_start(label, true, true, 0);

the widget won’t show up, on further inspection using the GTK Inspector, I did find out that in fact there is no widget inside the PreviewBox container.

I did call .show_all() on the top level, however in another function (even thought, since the widget is not actually present that is definitely not the problem).

Essentially, this should be the same code, but no dice.

As a side note
I have noticed another major difference that I belive could be related to this issue when moving from Python to C++.
In Python whenever I was getting a Window from a builder to then destroy it, I needed to get a callback on the destroy event signal, to prevent the window from beign destroyed and instead hide it, otherwise re opening (or rather, showing) the window would show an empty window.

In C++ instead there is no need to do any of this, and I believe this is related because it almost feel like in Python you can edit widgets inside a builder loaded from a UI file, while in C++ you cannot, thought, this doesn’t make much sense.

Thank’s in advance for any help!

EDIT:
As a comment that I forgot to add.
From my (potentially completely wrong) understanding, Gtk::Builder creates an instance of every widget inside the XML… it almost feels like Gtk::Builder::get_widget() returns a copy of said widget rather than a reference of the widget that will get drawn, while in Python a reference to the widget to be drawn is returned.

Is this assumption correct? And if so, how could I access to the widget directly stored within Builder that will actually be drawn?

I found the solution to this problem, and I must indeed apologize for my lack of thinking and high stupidity.

Essentially, due to the widget getting out of scope, GTK could never draw anything, because it had a null reference. Which totally makes sense.

In my defense, it would be useful to have a system to inform the user of such problems, but yup. Found the solution:

Gtk::Label* label = new Gtk::Label("test label");
previewBox->pack_start(*label, true, true, 0);
// Should delete that to avoid memory leak of course

I do hope this can potentially help someone someday, even thought, admittedly, this is not a GTK question nor a GTK binding question, it was a Cpp problem.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.