0

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!

NintendoZaedus
  • 653
  • 3
  • 8
  • 22
  • 1
    I am so sorry! On closer inspection, it seems you were right. I'm also unsure how I got widgets to show, I'm guessing a quirk of Gtkmm4, but thank you! You can repost your original solution, sorry for the inconvenience. I'm still unsure *why* adding it to the headerbar and setting it as the window's child changes how signals are handled, but I guess lesson to whoever might be me in the future, add all widgets to the Window as members. – NintendoZaedus Dec 27 '21 at 21:24

1 Answers1

1

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...

BobMorane
  • 3,870
  • 3
  • 20
  • 42