问题
In C, I can autoconnec signals with this code:
gtk_builder_connect_signals (builder, NULL)
How to do this in C++ with GTKmm?
回答1:
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
回答2:
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)
来源:https://stackoverflow.com/questions/20584614/auto-connection-signals-with-gtkbuilder-but-on-gtkmm