我正在尝试添加到 gtk::ListBox
来自无关小部件的事件处理闭包中的容器。有问题的列表框是通过 gtk::Builder
获取的。像这样:
let notes_list: gtk::ListBox = builder.get_object(NOTES_LIST_BOX_ID).unwrap();
还有我似乎无法添加到
notes_list
的事件处理程序(请注意,我尝试过不使用 clone!
宏、强引用和弱引用、包裹 Rc
指针等,但似乎没有任何改变):open_menu_item.connect_activate(clone!(@strong state, @strong notes_list => move |_| {
println!("Open database menu item activated");
// Seemingly can't add to notes_list from within this closure???
notes_list.add(>k::Label::new(Some("TEST"))); // Doesn't work???
let dialog = gtk::FileChooserDialog::with_buttons::<gtk::Window>(
Some("Open database file"),
None,
gtk::FileChooserAction::Open,
&[("_Cancel", gtk::ResponseType::Cancel),
("_Open", gtk::ResponseType::Accept)]
);
dialog.connect_response(clone!(@weak state, @weak notes_list => move |this, res| {
if res == gtk::ResponseType::Accept {
let file = this.get_file().unwrap();
let path_buf = file.get_path().unwrap();
println!("Opening database file: {}", path_buf.as_path().display());
let mut state = state.borrow_mut();
state.db = database::database_in_file(path_buf.as_path()).ok();
state.update_notes_list(¬es_list);
}
this.close();
}));
dialog.show_all();
}));
没有出现错误信息 - 没有出现预期的行为(即添加
gtk::Label
到列表框)。该模块的完整代码(以及我杂乱的代码库的其余部分):https://github.com/WiredSound/nos/blob/master/src/gui.rs
如果有人能帮我解决这个问题,我将不胜感激,谢谢。
最佳答案
GTK3 中的小部件默认是隐藏的。这意味着调用 show_all()
在容器上将显示其所有当前子项。如果您添加一个新 child ,您有责任调用 show()
以使其可见。
在您的信号处理程序中,您添加了 gtk::Label
到列表框,但您还需要使其可见。
关于rust - 尝试从 GTK-RS 应用程序的事件处理程序中添加到 `gtk::ListBox` 时似乎没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61444317/