我正在尝试将超时处理程序连接到我的gtkmm代码,如gtkmm book所示。但是,我特定的on_timeout()函数不需要任何参数,并且在遇到以下错误(以及其他错误)时,我正努力创建正确的sigc::slot
对象以传递给connect
函数:
error: no matching function for call to ‘bind(sigc::bound_mem_functor0<bool, DerivedWindow>)
还有几个
candidate expects 2 arguments, 1 provided
参考
sigc::bind
。我如何调用这两个函数:_timeout_slot = sigc::bind(sigc::mem_fun(*this,&DerivedWindow::on_timeout));
_connection = Glib::signal_timeout().connect(_timeout_slot,timeout_value);
我正在从
DerivedWindow
派生的类Gtk::Window
上执行此操作。我到底在做什么错?如果不需要任何参数,是否需要使用sigc::bind
和sigc::mem_func
? 最佳答案
这里不需要sigc::bind
,因为您没有将任何其他参数绑定到插槽(this
已经解决了为sigc::mem_fun
引用成员函数指针的问题)。因此,这就足够了:
_timeout_slot = sigc::mem_fun(*this, &MyWindow::on_timeout)
_connection = Glib::signal_timeout().connect(_timeout_slot, timeout_value);
快速提示:如果可以使用C ++ 11,则可以将lambda作为传递参数进行连接,这使内容更易读:
_connection = Glib::signal_timeout().connect([this]{ return on_timeout(); }, timeout_value);
为此,您可能需要添加
namespace sigc{
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
另外,如果要连接到类实例的信号(例如
Gtk::Button* btn
),则可以通过定义宏使事情变得更加紧凑。#define CONNECT(src, signal, ...) (src)->signal_##signal().connect(__VA_ARGS__)
然后可以让你写
CONNECT(btn, clicked, [this]{ btn_clicked_slot(); });
关于c++ - 创建不带参数的sigc::slot,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22563621/