Auto-connection signals with GtkBuilder but on GTKmm

Viewed 2416

In C, I can autoconnect signals with this code:

gtk_builder_connect_signals (builder, NULL)

How to do this in C++ with GTKmm?

2 Answers

Of course you can, there is nothing wrong with mixing C and C++ code.

here is an example code that assumes the signal handler onComboBoxSelectedItemChange is set from glade on a GtkComboBox.

#include <gtkmm.h>
#include <string>

namespace GUI{

int init(){
    auto app = Gtk::Application::create();
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("./res/GUI.glade");
    gtk_builder_connect_signals(builder->gobj(), NULL);

    Gtk::Window* mainWindow = nullptr;

    builder->get_widget("mainWindow", mainWindow);
    return app->run(*mainWindow);
}

extern "C"
void onComboBoxSelectedItemChange(GtkComboBox *widget, gpointer user_data){
    int selectedIndex = gtk_combo_box_get_active(widget);
    Gtk::MessageDialog dialog(std::to_string(selectedIndex).c_str());
    dialog.run();
}

}

int main(){
    return GUI::init();
}

you can build using

g++ -rdynamic -std=c++11 test.cpp $(pkg-config --cflags --libs gtkmm-3.0)
Related