我想将处理程序连接到自定义信号。在我的空地档案里,我有一扇带按钮的窗户。窗口在Rust:中加载如下
let glade_src = include_str!("views/window.glade");
let builder = gtk::Builder::new_from_string(glade_src);
let window: gtk::ApplicationWindow = builder.get_object("Window").expect("Couldn't get window view");
window.set_application(Some(app));
此窗口中的按钮定义了以下信号:
<signal name="clicked" handler="_on_start_clicked" swapped="no"/>
在Python中,我可以使用简单的方法注释进行连接:
@Gtk.Template.Callback()
def _on_start_clicked(self, sender):
print("start clicked")
但是,我如何将Rust中的函数连接到这个信号?
AFAIK我在Rust中没有这样的注释。我需要window.connect_signal("_on_start_clicked", handler);
之类的东西
我用的是Rust和gtk板条箱。
这似乎是非常新的。拉取请求实际上并不在gtk机箱的稳定版本中(截至v0.7.0(。通过直接使用git repo,我设法使用Builder::connect_signals()
连接信号,如下所示:
// the handler
fn on_start_clicked(param: &[glib::Value]) -> Option<glib::Value> {
println!("on_start_clicked fired!");
None
}
// ...
// connect all signals
builder.connect_signals(|builder, handler_name| {
match handler_name {
// handler_name as defined in the glade file => handler function as defined above
"_on_start_clicked" => Box::new(on_start_clicked),
_ => Box::new(|_| {None})
}
});
如果仍未稳定发布,请在Cargo.toml文件中使用此依赖项:
[dependencies]
glib = { git = "https://github.com/gtk-rs/glib.git" }
[dependencies.gtk]
git = "https://github.com/gtk-rs/gtk.git"
features = ["v3_22"]