Container object appears in its own separate window

First a brief outline:
I am writing an application that requires several controls such as buttons, text entry and display objects using GTK3 and gtkmm. The UI XML is developed with GNOME. The problem I am facing is that the ‘Layout’ container I am using is appearing as a separate and independent window outside of the application window. A stripped out version of the code which demonstrated the problem is as follows;
<------------------------------------------------------------------------------->

#include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/button.h>
#include <gtkmm/entry.h>
#include <gtkmm/layout.h>
#include <gtkmm/builder.h>

using namespace Glib;
using namespace Gtk;

class UIWindow : public Gtk::ApplicationWindow {
    Window *window;
    Layout *grid;
    RefPtr<Button> display_btn;
    RefPtr<Entry> ipAddrEntry;
    RefPtr<Builder> ui;
    
public:
    UIWindow()
    : ui{Builder::create_from_file("gtk_basic.glade")} {
        if(ui) 
        {
            ui->get_widget<Window>("window1", window);
            ui->get_widget<Layout>("layout2", grid);
                        
            display_btn = Glib::RefPtr<Button>::cast_dynamic(
                ui->get_object("button1"));
            
            ipAddrEntry = RefPtr<Entry>::cast_dynamic(
                ui->get_object("clientIpAddress"));
            
            if(window && ipAddrEntry && display_btn) 
            {
              display_btn->signal_clicked().connect(
              [this]() {
                  ipAddrEntry->set_text("Hello World");
              });
              add(*window);
              set_title("Simple Gtk::Builder demo");
            }
        }
        set_default_size(400, 400);
        show_all();
    }
};

int main(int argc, char *argv[]) {
    RefPtr<Application> app = Application::create(argc, argv);
    UIWindow UIW;
    return app->run(UIW);
}

<---------------------------------------------------------------------------------------->
And this is the XML (File name gtk_basic.glade);

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLayout" id="layout2">
        <property name="width_request">100</property>
        <property name="height_request">80</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkEntry" id="clientIpAddress">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="editable">False</property>
            <property name="invisible_char">●</property>
            <property name="width_chars">26</property>
            <property name="truncate_multiline">True</property>
            <property name="primary_icon_activatable">False</property>
            <property name="secondary_icon_activatable">False</property>
          </object>
          <packing>
            <property name="x">10</property>
            <property name="y">43</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">102</property>
            <property name="height_request">45</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="x">10</property>
            <property name="y">147</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

<---------------------------------------------------------------------------------------->

Running the program from Geany three windows appear; the script window, an empty application window and a totally separate and fully functional window consisting of the ‘Layout’ container and the controls. The ‘Layout’ window can be resized, minimised, closed etc. and the controls function as expected. The script widow shows the following runtime warning:
“(gtk_basic:4846): Gtk-WARNING **: 09:59:31.627: Can’t set a parent on a toplevel widget”
Closing the ‘Layout’ window has no effect on the application window but closing the application window also closes down the ‘Layout’ window as may be expected.
Removing the call to <add(*window)> removes the runtime warning but then only the empty Application window is displayed, the ‘Layout’ container and controls do not appear anywhere. I’m sure the answer is staring me in the face but I just can’t see it.

Addendum: If I replace <add(window)> with <add(grid)> I get the warning:
(gtk_basic:16174): Gtk-WARNING **: 12:53:54.427: Attempting to add a widget with type gtkmm__GtkLayout to a container of type gtkmm__GtkApplicationWindow, but the widget is already inside a container of type gtkmm__GtkWindow, please remove the widget from its existing container first.

So something somewhere is creating a new window to contain the Layout.

You create a window in code (your class inherits from GtkApplicationWindow) and your UI file contains another GtkWindow, defined via XML.

You probably wanted to define the entire contents of your window class in that UI file, in that case look up “gtk composite templates”.

I knew it was staring me in the face. I wanted to derive my UIWindow class from the ApplicationWindow class in order to keep it all contained and to be able to use the Menubar functionality. My derived class will contain far too much functionality to embed into XML. Information on composite templates appears very limited and not very clear. Thanks for the input.

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