rust - 尝试从 GTK-RS 应用程序的事件处理程序中添加到 `gtk::ListBox` 时似乎没有任何反应

标签 rust gtk gtk-rs

我正在尝试添加到 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(&gtk::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(&notes_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/

相关文章:

linux - 在 Linux 中使用 gtk 显示一系列图像

rust - 我们如何将 Rust 字符串转换为 gtk::type::String?

rust - 在 GTK4 中制作可点击框

c - 是否可以在 Rust 中包装 C 枚举?

rust - 带有参数的闭包上的 "Closure takes 0 arguments"

Python QT (PySide) QPushButton 样式表导致 "assertion ' GTK_IS_WIDGET(小部件 )' failed"错误

multithreading - gtk-rs:如何从另一个线程更新 View

vector - 分割与Vec <u8>模式匹配的Vec <u8>

generics - 为什么 impl 不在范围内

ubuntu - 使用 pyatspi 关闭窗口