Search code examples
c++gtkmm3

Gtkmm 3/C++, closing the program with a button instead of the window "X"


everybody.

I am working on a gtkmm app and need some help getting a "Close" button to work. As suggested by the gtkmm documentation, I derived a class for the main window object, created some members, and left the main() function mostly for reading the glade UI file, instantiating the form and starting the main loop.

There are 3 files, named conveniently for explanation: Declarations.h, Declarations.cpp, Program.cpp

In "Declarations.h" I have the class inherited from the Gtk Window:

#include <gtkmm.h>

class MainWindowClass : public Gtk::ApplicationWindow
{
protected:
    Gtk::Button *button_close;
    // other buttons here

protected:
    void on_button_close_clicked();
    // other callback functions here
public:
    MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade); // Constructor
    // Destructor, other public members
};

In "Declarations.cpp" I have the implementations:

#include "Declarations.h"

using namespace Gtk;

// Implementing the constructor
MainWindowClass::MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade) :
    Gtk::Window(cobject), builder(refGlade)
{
    builder->get_widget("button_close", button_close);
    // Getting other widgets from the glade file

    button_close->signal_clicked().connect(sigc::mem_fun(*this, &MainWindowClass::on_button_close_clicked));
    // Connecting other callback functions here
}

// Implementing the callback for the "Close" button, ** PROBLEM IS HERE **
void MainWindowClass::on_button_close_clicked()
{
    //gtk_main_quit(); Apparently GTK+/C only, compiler doesn't complain but causes a segfault when clicking the button
    //Gtk::Application::quit(); Won't compile
}

The Program.cpp reads the UI from a file and starts the main program loop:

#include <gtkmm.h>
#include "Declarations.h"

int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv, "Damn this close button");

    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("Program_UI.glade");

    MainWindowClass our_main_window;

    return app->run(our_main_window);
}

I am omitting some non-relevant code (of other objects and callbacks) because they work, it is the close procedure that is causing me trouble, though closing the app with "X" works. I have also thought about trying to call a quit() or destroy() function (if they exist) of "app", but then the callback function doesn't know "app" exists. What do you guys suggest?

Thanks a lot.


** Edit: fixed this using FormMain::hide(), which is inherited from GtkWindow. I thought the static procedure Gtk::Main::hide() would do it, but the compiler says that hide() is not a member of Gtk::Main... Well, moving forward one step at a time.


Solution

  • Used FormMain::hide() (inherited from GtkWindow). The static procedure Gtk::Main::hide() was not being recognized by the compiler.