Search code examples
gtkgtkmmgtk4gtkmm4

gtkmm4: Gtk::Button click signal doesn't fire when in Gtk::HeaderBar


I'm using gtkmm4. I have a Gtk::Window, Gtk::Button, and Gtk::HeaderBar. I've packed the Button to the end of the HeaderBar and then set the Window's titlebar to the headerbar. My code looks something like this:

class Window: public Gtk::ApplicationWindow
{

public:
    
    Window(){
        Gtk::Box box;
        Gtk::HeaderBar bar;
        Gtk::Button button{"Test Button"};

        button.signal_clicked().connect(sigc::mem_fun(*this, &Window::on_button_pressed));

        set_titlebar(bar);
        set_child(button);
    };

private:
    void on_button_pressed()
    {
        std::cout << "Button clicked!" << std::endl;
    };
};

For some reason the button's click signal doesn't activate when I click on it when I would expect it would. Is there anything I'm doing wrong here? Thank you in advance!


Solution

  • According to your example, when you write:

    Gtk::Button button{"Test Button"};
    

    the button variable is local and dies by the end of the Window constructor. To avoid this, you have two choices:

    1. Make is a Window class member.
    2. Use Gtk::make_managed to allow Window to automatically handle the lifespan of its child button.

    I'm surprised you even see the widgets at all... I suspect the real code is different...