Garmaine Staff asked 1 year ago

I am using Gtkmm3 (Ubuntu) to make a small GUI application. In this application, I have a couple windows to create which basically all follow the same pattern:

  1. registering a main layout (Gtk::Grid);
  2. configuring window properties (icon, title, etc);
  3. configuring layout properties (expanding, sub-layouts, etc);
  4. configuring window widgets (adding them to layouts, labels, etc).

In hope of not having to rewrite all of this logic every time I create a new window, I have written the following base class:

template<typename GtkmmWindow>
class Window



    virtual int Show() = 0;


    virtual void ConfigureWindow() = 0;
    virtual void ConfigureLayouts() = 0;
    virtual void ConfigureWidgets() = 0;

    void Init();

    Gtk::Grid m_mainLayout;
    GtkmmWindow m_window;


template<typename GtkmmWindow>

    // When signal 'realize' is sent, 'Init' will be triggered.
    // This happens after construction, so virtual methods can
    // be used safely:

// Initialize child window according to its own needs:
template<typename GtkmmWindow>
void Window<GtkmmWindow>::Init()

    // If this line is removed, no widgets are shown.

The goal of this class is to make sure points 1 to 4 are implemented by all windows the same way. It does so by calling appropriate virtual methods (to be redefined in concrete child classes) when the realize signal is sent. This is because when the realize signal is sent, I know the window constructors have been called and that I can safely use virtual methods.

For example, here is how I use it to create an application main window:

class MyWindow : public Window<Gtk::ApplicationWindow>


    MyWindow(Gtk::Application& p_app) : m_app{p_app} {}

    int Show() override


    Gtk::Application& m_app;
    Gtk::Button m_button;

    void ConfigureWindow() override
        m_window.set_title("SO Question");

        // If I set this to false, the window shrinks to fit the button size:

    void ConfigureLayouts() override

    void ConfigureWidgets() override
        m_mainLayout.attach(m_button, 0, 0, 1, 1);


This main window sets the main layout to have a yellow background, has a Gtk::Button with label "Hello" registered in the main layout. The problem I have with this strategy is that when I run the code, I get weird window/layout sizing:

enter image description here

Notice the yellow layout is way bigger than the only widget (the button) contained in it. This is the result I would have expected:

enter image description here

That is, the window and main layout should shrink to the size of their only contained widget. Weirdly, if I make the window set_resizable(false), I get the sizing I want, but then I can't resize it anymore, which is often not acceptable.


  1. Why is it no so?
  2. How can I achieve this without duplicating the base code for every window?

You can build this code using g++ by adding it to:

#include <memory>
#include <gtkmm.h>

// Add here...

int main(int argc, char *argv[])
  auto app = Gtk::Application::create(argc, argv, "so.realize");

  std::unique_ptr<MyWindow> mainWindow = std::make_unique<MyWindow>(*(app.get()));

  return mainWindow->Show();

and running:

g++ -std=c++17 main.cpp -o example.out `pkg-config gtkmm-3.0 --cflags --libs`