如何在Rust的gtk-rs绑定中的ListStore中添加ListStore?

时间:2016-07-30 17:40:12

标签: combobox rust gtk3 treemodel rust-gnome

我想使用gtk-rs bindings创建包含TreeViewComboBox的程序。

为了获得GtkCellRendererCombo的数据,我需要GtkTreeModel *,但我不知道如何在ListStore内添加ListStore

现在我只有一个简单的TreeView。我想要一个ComboBox里面的1列(标题" f64"):

extern crate gtk;

use gtk::prelude::*;
use gtk::{ComboBox, ListStore, Orientation, TreeView, TreeViewColumn, Window, WindowType};

fn main() {
    gtk::init().expect("Failed to initialize GTK");
    let window = Window::new(WindowType::Toplevel);
    window.set_position(gtk::WindowPosition::Center);
    window.set_title("Rust&GTK+3");
    window.set_position(gtk::WindowPosition::Center);
    window.connect_delete_event(|wind, _| {
        let (szerokosc, wysokosc) = wind.get_size();
        println!("Width {} Height {}", szerokosc, wysokosc);
        // Stop the main loop.
        gtk::main_quit();
        // Let the default handler destroy the window.
        Inhibit(false)
    });
    let main_box = gtk::Box::new(Orientation::Vertical, 0);
    let view_list = TreeView::new();
    {
        let types_inside_columns = &[gtk::Type::U32, gtk::Type::F64];
        let model_list_of_data = ListStore::new(types_inside_columns);
        for liczba in 0..10 {
            let array_of_data = [&(liczba.to_value()) as &ToValue,
                                 &((liczba as f64 * 1.5).to_value()) as &ToValue];
            model_list_of_data.insert_with_values(None, &[0, 1], &array_of_data);
        }
        view_list.set_model(Some(&model_list_of_data));
        let object_to_render_cells: gtk::CellRendererText = gtk::CellRendererText::new();
        object_to_render_cells.set_visible(true);
        let view_column = TreeViewColumn::new();
        view_column.set_expand(true);
        view_column.set_visible(true);
        view_column.set_title("u32");
        view_column.pack_start(&object_to_render_cells, true);
        view_column.add_attribute(&object_to_render_cells, "text", 0);
        view_list.append_column(&view_column);
        // second column
        let object_to_render_cells_2: gtk::CellRendererText = gtk::CellRendererText::new();
        object_to_render_cells_2.set_visible(true);
        let view_column_2 = TreeViewColumn::new();
        view_column_2.set_expand(true);
        view_column_2.set_visible(true);
        view_column_2.set_title("f64");
        view_column_2.pack_start(&object_to_render_cells, true);
        view_column_2.add_attribute(&object_to_render_cells, "text", 1);
        view_list.append_column(&view_column_2);
    }
    view_list.expand_all();
    main_box.add(&view_list);
    window.add(&main_box);
    window.show_all();
    gtk::main();
}

我尝试将gtk::Type:BaseBoxed添加到types_inside_columns,然后将ListStore添加到array_of_data,但Box无法通过方法{{}成功转换1}}输入to_value()

添加了: 这个程序在运行时很恐慌:

ToValue

1 个答案:

答案 0 :(得分:2)

键入问题

如果您想在另一个gtk::ListStore中存储gtk::ListStore,则不指定gtk::Type::Pointer,您需要使用最后一个版本gtk::Type::Other(usize) 。您可以在运行时使用ToValue::to_value_type获取gtk::ListStore的类型。遗憾的是,在编译时找不到一种方法,因为类型号会改变。

let model = ListStore::new(&[Type::U8]);
println!("{:?}", model.to_value_type()); // Other(139977306752544)
println!("{:?}", model.to_value()); // Value(((GtkListStore*) 0x7f4f01b9a060))
// get type at runtime
let liststore_type = model.to_value_type();
let model = ListStore::new(&[Type::U32, Type::F64, liststore_type]);

现在,您可以根据需要插入子模型ToValue::to_value

使用gtk::CellRendererCombo

但是,您写道,您希望连续使用组合框,可能用于编辑数字。为此,您不需要嵌套gtk::ListStore。您需要gtk::TreeView的模型和选项的另一个模型。

let model_list_of_data = ListStore::new(&[gtk::Type::U32, gtk::Type::String]);
let model_for_combo = ListStore::new(&[gtk::Type::String]);


for liczba in 0..10 {
    let integer = liczba.to_value();
    let float = format!("{}", liczba as f64 * 1.5).to_value();

    model_list_of_data.insert_with_values(None, &[0, 1], &[&integer, &float]);
    model_for_combo.insert_with_values(None, &[0], &[&float]);
}

// code for first column not changed

let object_to_render_cells_2: gtk::CellRendererCombo = gtk::CellRendererCombo::new();
object_to_render_cells_2.set_visible(true);
object_to_render_cells_2.set_property_editable(true);
// use the combo model for the options
object_to_render_cells_2.set_property_model(Some(&model_for_combo.upcast()));
// display the options of the first column in the combo model
object_to_render_cells_2.set_property_text_column(0);
let view_column_2 = TreeViewColumn::new();
view_column_2.set_expand(true);
view_column_2.set_visible(true);
view_column_2.set_title("f64");
view_column_2.pack_start(&object_to_render_cells_2, false);
// the data for each row is in the second column in the tree model
view_column_2.add_attribute(&object_to_render_cells_2, "text", 1);
view_list.append_column(&view_column_2);

您可能已经注意到我使用了字符串而不是普通浮点数。这有两个原因:

  1. 使用format!,输出中不包含不必要的零
  2. 使用普通浮动,gtk将显示组合,但如果单击一个
  3. 将崩溃

    screenshot