Question

I'm trying to connect a timeout handler to my gtkmm code as shown in gtkmm book. However, my particular on_timeout() function doesn't need any arguments, and I'm struggling to properly create a sigc::slot object to pass to the connect function, as I encounter the following error (among others):

error: no matching function for call to ‘bind(sigc::bound_mem_functor0<bool, DerivedWindow>)

and several

candidate expects 2 arguments, 1 provided

referring to sigc::bind. How I am calling both functions:

_timeout_slot = sigc::bind(sigc::mem_fun(*this,&DerivedWindow::on_timeout));
_connection = Glib::signal_timeout().connect(_timeout_slot,timeout_value);

I'm doing this on a class DerivedWindow derived from Gtk::Window. What exactly am I doing wrong? Do I need to use sigc::bind and sigc::mem_func if I don't require any arguments?

Was it helpful?

Solution

You don't need sigc::bind here since you are not binding any additional arguments to the slot (dealing with dereferencing the member function pointer for this is already taken care of by sigc::mem_fun). So, this is sufficient:

_timeout_slot = sigc::mem_fun(*this, &MyWindow::on_timeout)
_connection = Glib::signal_timeout().connect(_timeout_slot, timeout_value);

A quick tip: if you can use C++11, you can just pass lambdas as arguments to connect, which makes things more readable:

_connection = Glib::signal_timeout().connect([this]{ return on_timeout(); }, timeout_value);

For this to work, you may need to add

namespace sigc{
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}

Also, if you want to connect to signals of a class instance (say a Gtk::Button* btn), you can make things even more compact by defining a macro

#define CONNECT(src, signal, ...) (src)->signal_##signal().connect(__VA_ARGS__)

which then allows you to write

CONNECT(btn, clicked, [this]{ btn_clicked_slot(); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top