Question

In C, I can autoconnect signals with this code:

gtk_builder_connect_signals (builder, NULL)

How to do this in C++ with GTKmm?

Was it helpful?

Solution

You cannot use Glade to connect your signals when using gtkmm, you need to do that manually.

    Glib::RefPtr builder = Gtk::Builder::create_from_file("glade_file.ui");

    Gtk::Window *window1 = 0;
    builder->get_widget("window1", window1);

    Gtk::Button *button1 = 0;
    builder->get_widget("button1", button1);
    // get other widgets
    ...

    button1->signal_clicked().connect(sigc::mem_fun(*this, &button1_clicked));

Have a look at these answers :

https://stackoverflow.com/a/3191472/1673000

https://stackoverflow.com/a/1637058/1673000

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top