Question

I make my interface with glade. Works great. I write a tiny little main function which calls gtkbuilder and renders everything in the glade file.

Gtk::Main kit(num, opts);
// Load the GtkBuilder file and instantiate its widgets:
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("dsg.glade", "mainwindow");

Works even better. Then I get my widget

Gtk::Button *sf = 0;
builder->get_widget("button", sf);

Now what? Every example I've found to connect a signal handler is not built by gtkbuilder. If you instantiate your own class to represent/handle/render a button it's easy to connect a signal handler to it, but I'm using gtkbuilder and I don't see how to write a function that I can then attach to my widget, since I wasn't the one creating the button object, gtkbuilder was.

Do I make a subclass of gtkbutton write my function then point to that? But my class isn't being instantiated by gtkbuilder.

I just don't get it. Help?

Was it helpful?

Solution

You're not missing something obvious. It seems that gtkmm doesn't provide a C++ version of the gtk_builder_connect_signals() function, which is how you do it in C. I've done a little Google searching, but I can't figure out why they would leave it out.

You can access the C function directly like this:

Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("dsg.glade", "mainwindow");
gtk_builder_connect_signals(builder->gobj(), callback_data);

OTHER TIPS

you should use your own button class like this:

#ifndef _MYBUTTON_H_
#define _MYBUTTON_H_

#include <gtkmm.h>
class MyButton : public Gtk::Button { 
  public:
    inline MyButton(BaseObjectType* cobject) : Gtk::Button(cobject) {};
  protected:
    // overwrite virtual void on_pressed() function of Gtk::Button
    void on_pressed() { /* do something */ };
};
#endif

Now somewhere in your main code (I put it in my Core class who is of public of Gtk::Window):

MyButton *sf;
refBuilder_->get_widget_derived("button", sf);

I found this in the official gtkmm manual some month ago and using it successfully in my GUI applications.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top