Do I have to manually attach a gtk signal handler when I specified the function name in glade?

StackOverflow https://stackoverflow.com/questions/4089077

  •  28-09-2019
  •  | 
  •  

Question

I'm writing my first gtk program, using gtkmm, and glade. I made a filechooserbutton and it has a signal called file-set So I set that to what I assume is the function name I want it to call when the file is chosen. But then I see here: http://library.gnome.org/devel/gtkmm-tutorial/unstable/sec-builder-accessing-widgets.html.en

That they're manually getting the dialog widget and setting a button signal handler in the code. Which is the right way to do it?

And while I'm here any links to good examples would be handy, they seem to be few and far between. Thanks.

Was it helpful?

Solution

This is how I did it:

// create the UI
refUI = Gtk::Builder::create();
refUI->add_from_file(grq::GLADE_FILE);

// grab your widget
refUI->get_widget("but_new", but_new); // Gtk::ToolButton *but_new;
but_new->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_new_game));

// your signal handler looks something like this :)
void MainWindow::on_new_game() {}

edit:

Basically the *this is the object on which you will be calling the function your signal handler.

This is what my main looks like:

int main(int argc, char **argv) {

    Gtk::Main       kit(argc, argv);
    MainWindow      main_window;

    kit.run(*main_window.window);

    return 0;
}

MainWindow is basically a class that wraps GtkWindow and defines the widgets, a. la.:

class MainWindow
{

private:
Glib::RefPtr<Gtk::Builder> refUI;

//
// Widgets
//

Gtk::ToolButton *but_about;

public:

// The window. This is public so we can hook into events and
// call kit.run(window) against it, if needed.
Gtk::Window *window;


MainWindow()
{
    // Load the data for this window and it's widgets.
    refUI = Gtk::Builder::create();
    refUI->add_from_file(grq::GLADE_FILE);


    // The window
    refUI->get_widget("main_window", window);


    // Widgets              
    refUI->get_widget("but_about", but_about);
    but_about->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_about));

            ...

}


virtual ~MainWindow()
{
    if (window != NULL)
    {
        delete window; // Frees all the children for the window, too.
    }
}

    virtual void on_about()
    {
            // stuff
    }

};

Hope this helps!

OTHER TIPS

I found the answer to my question as an afterthought in another stackoverflow question.

But I don't remember which one it was.

The answer seems to be that you have to programmatically add the signal handler to the widget in your code, the gtkbuilder won't do it for you.

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