我想使用gtk-rs bindingsTreeView
创建包含ComboBox
es 的程序。
为了获得 的数据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>K+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
totypes_inside_columns
然后添加一个boxed ListStore
to array_of_data
,但Box
无法通过方法成功转换to_value()
为type ToValue
。
补充:该程序在运行时出现恐慌:
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>K+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,gtk::Type::Pointer];
let model_list_of_data = ListStore::new(types_inside_columns);
for liczba in 0..10 {
let model_for_combo = ListStore::new(types_inside_columns);
let array_of_data = [&(liczba.to_value()) as &ToValue,
&((liczba as f64 * 1.5).to_value()) as &ToValue,
];
let typ_iter = model_list_of_data.insert_with_values(None,&[0,1],&array_of_data);
model_list_of_data.set_value(&typ_iter,2, &model_for_combo.to_value() as >k::Value);
}
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::CellRendererCombo = gtk::CellRendererCombo::new();
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,true);
view_column_2.add_attribute(&object_to_render_cells_2,"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();
}