我正在使用 Rust 和 GTK+ 3 的绑定(称为 Gtk-rs 或 rust-gnome)创建 GUI 应用程序。我想使用方法将一些数据插入gtk::ListStore
(GtkListStore
)insert_with_values
工作示例,但只有一列: 如果我有一列,我可以插入数据:
let type_of_a_column = &[gtk::Type::U32];
let data_for_column_1 = (2 as u32).to_value();
let model_list_of_data = ListStore::new(type_of_a_column);
model_list_of_data.insert_with_values(None, &[0 as u32], &[&data_for_column_1]);
但是,如果我有更多列,我不知道如何交出更多数据。我知道它应该在内部调用gtk_list_store_insert_with_valuesv() 所以我可能需要一个数组或切片,但是如果我执行以下操作:
想在数组中传递两个元素,不工作
let type_of_a_column = &[gtk::Type::U32, gtk::Type::I64];
let data_for_column_1 = (2 as u32).to_value();
let data_for_column_2 = (4 as i64).to_value();
let array_of_data = [&data_for_column_1, &data_for_column_2];
let model_list_of_data = ListStore::new(type_of_a_column);
model_list_of_data.insert_with_values(None, &[1 as u32], &[&array_of_data]);
它在编译期间给了我:
错误:
[&glib::Value; 2]: glib::IsA<glib::Object>
不满足特征界限 [E0277]
我不知道如何克服它。
不工作 MCVE
main.rs
extern crate gtk;
use gtk::prelude::*;
use gtk::{Box, ListStore, Orientation, TreeView, Window, WindowType};
fn exit_app() -> gtk::prelude::Inhibit {
// Stop the main loop.
gtk::main_quit();
// Let the default handler destroy the window.
Inhibit(false)
}
fn main() {
if gtk::init().is_err() {
panic!("Failed to initialize GTK.");
}
println!("Version of GTK+3:\t{}.{}",
gtk::get_major_version(),
gtk::get_minor_version());
let window = Window::new(WindowType::Toplevel);
window.set_title("Exercises with GtkTreeView and GtkListStore");
window.set_position(gtk::WindowPosition::Center);
window.connect_delete_event(|_, _| exit_app());
let box_container = Box::new(Orientation::Vertical, 0);
let view_list = TreeView::new();
let types_inside_columns = &[gtk::Type::U32, gtk::Type::I64];
let data_in_column_1 = (2 as u32).to_value();
let data_in_column_2 = (4 as i64).to_value();
let array_of_data = [&data_in_column_1, &data_in_column_2];
let model_list_of_data = ListStore::new(types_inside_columns);
model_list_of_data.insert_with_values(Some(0), &[0 as u32, 1 as u32], &[&array_of_data]);
view_list.set_model(Some(&model_list_of_data));
box_container.pack_start(&view_list, false, false, 0);
window.add(&box_container);
window.show_all();
gtk::main();
}
货运.toml
[package]
name = "test_of_gtk_3"
version = "0.0.1"
authors = ["wm_obsd"]
[dependencies.gtk]
version = "0.1.0"
features = ["v3_16"]
[[bin]]
name = "main"
path = "src/main.rs"