How to add pages to GtkStack (not form XML file)

I am having a GtkStack with one page defined from a XML file. That particular stack and stack page works.
Later on (in a .so added with g_module_open), I want to add pages to that GtkStack.

Tried various things, this being the latest,

 <?xml version="1.0" encoding="UTF-8"?>
	<requires lib="gtk+" version="4.00"/>
	<object class="GtkStack">
		<property name="transition-type">crossfade</property>
			<object class="GtkStackPage" id="page">
				<property name="name">page1</property>
				<property name="title" translatable="yes">XXX</property>
				<property name="child">
					<object class="GtkImage">
						<property name="icon-name">org.gtk.Demo4</property>

where in that .so I tried to:

my_preferences = GTK_STACK_PAGE (gtk_builder_get_object (builder, "page"));
GtkWidget*	parent = gtk_stack_page_get_child (my_preferences);
gtk_stack_add_child (preferences, parent);

“preferences” is a stack ( preferences = GTK_STACK (gtk_builder_get_object (builder, "pref-stack")); )

The error I get is:

Gtk-CRITICAL **: 14:23:06.356: gtk_widget_set_parent: assertion '_gtk_widget_get_parent (widget) == NULL' failed

Obviously, I don’t know what I am doing. Stack pages are not derived from GtkWidget so how to do this? I have tried also to put a GtkBox on top of the GtkStack. The removes the error but no pages are added to the stack.

GtkWidget*	parent = gtk_stack_page_get_child (my_preferences);

I suggest you reexamine this line a little :slight_smile:

Remember you trying to a page to the stack

Yeah, that’s funny. It’s a typo. My bad. The code that is there right now is,

	my_preferences = GTK_STACK_PAGE (gtk_builder_get_object (builder, "sh-pref"));
	gtk_stack_add_child(preferences, gtk_stack_page_get_child (my_preferences));

Still doesn’t work but the widget that the stack page refers to seems to change one of the widgets in the stack that “preferences” refers to. I was (naively, I guess) expecting that I would get another page added to the stack “preferences”, not messing up widgets that I have on the other stack pages.

Wait I’m confused, so your trying to move the page from one stack to another?

:slight_smile: No, I have defined a stack and a page in the main program (as part of a menu). The main g_module_open:s a .so with its own UI that wants to add another page to that stack (the main has its preferences page and the linked .so wants to add its own preferences page to the same stack which is part of a menu).

“my_preferences” is:

static GtkStackPage*	my_preferences;

and preferences:

GtkStack* preferences = GTK_STACK (gtk_builder_get_object (builder, "pref-stack"));

is called earlier. In the xml for it, I have,

<object class="GtkStack" id="pref-stack">

And that seems to work. It’s the adding of a new page defined in the .so

	my_preferences = GTK_STACK_PAGE (gtk_builder_get_object (builder, "sh-pref"));
	gtk_stack_add_child(preferences, gtk_stack_page_get_child (my_preferences));

That seems to be altering one widget in a previously defined stack page instead of adding another page to that GtkStack.

The solution to my problem appears to be to just define a widget, I my case a Box and do all the GtkStackPage-related stuff in the code.

	last_visible = gtk_stack_get_visible_child (preferences);  /* this is just to restore current view */

	my_preferences = GTK_BOX (gtk_builder_get_object (builder, "my-pref"));
	GtkStackPage* my_page = gtk_stack_add_child(preferences, GTK_WIDGET (my_preferences));
	gtk_stack_page_set_title(my_page, "My Preferences");

If I don’t save last_visible, then when “my-pref” is removed, the “preferences” GtkStack shows nothing.

Maybe this helps somebody because there is not so much written about this that I could find.

But in your first xml snippet, ‘page’ is already within a GtkStack, and thus so is it’s child

Hence why trying to add it to a second stack you hit that assertion (i.e. it’s already got a parent)

Sorry for that. When I don’t get things to work, I go through a bunch of variations of how to interpret the API. This must have been a leftover from that.
The thing that solved it for me was realizing that gtk_stack_add_child returns GtkStackPage*.

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