当试图从GTK-RS应用程序的事件处理程序中添加到`gtk :: ListBox`中时似乎什么也没发生

问题描述 投票:1回答:1

我正在尝试从不相关的窗口小部件的事件处理闭包中添加到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

如果有人能帮助我解决这个问题,那么,我将非常感谢,谢谢。

rust gtk gtk-rs
1个回答
1
投票

GTK3中的小部件在默认情况下是隐藏的。这意味着在容器上调用show_all()将显示其所有current子代。如果添加一个新子项,则有责任对其调用show()以使其可见。

在信号处理程序中,您将gtk::Label添加到列表框中,但也需要使其可见。

© www.soinside.com 2019 - 2024. All rights reserved.