我正在尝试增加 Rust 和 GTK-RS 应用程序的结构,但我不知道如何处理事件连接。我发现问题出在错误的生命周期中,但我真的不明白如何解决它。
#[derive(Debug)]
struct CreatingProfileUI {
window: gtk::MessageDialog,
profile_name_entry: gtk::Entry,
add_btn: gtk::Button,
cancel_btn: gtk::Button,
}
#[derive(Debug)]
struct UI {
window: gtk::Window,
// Header
url_entry: gtk::Entry,
open_btn: gtk::Button,
// Body
add_profile_btn: gtk::Button,
remove_profile_btn: gtk::Button,
profiles_textview: gtk::TextView,
// Creating profile
creating_profile: CreatingProfileUI,
// Statusbar
statusbar: gtk::Statusbar,
}
impl UI {
fn init(&self) {
self.add_profile_btn
.connect_clicked(move |_| { &self.creating_profile.window.run(); });
}
}
我收到此错误:
error[E0477]: the type `[closure@src/main.rs:109:46: 111:6 self:&UI]` does not fulfill the required lifetime
--> src/main.rs:109:30
|
109 | self.add_profile_btn.connect_clicked(move |_| {
| ^^^^^^^^^^^^^^^
|
= note: type must satisfy the static lifetime
您不能将非静态引用移至 GTK 回调中。您需要静态的东西或堆分配的东西(例如在Box
/RefCell
/Rc
/etc.).
回调不是从连接到信号的范围调用的,而是在稍后的某个时刻从主循环调用的。要求您传递到闭包中的任何内容仍然有效,这可以是任何东西'static
、堆分配或在 main 和主循环运行位置之间的堆栈上分配。最后一部分目前无法用 Rust/GTK-rs 很好地表达。
See gtk-rs 文档底部的示例作为示例 http://gtk-rs.org/tuto/closures。它使用一个Rc<RefCell<_>>
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)