I am creating GUI application using Rust and bindings for GTK+ 3 (called Gtk-rs or rust-gnome). I want to insert some data into gtk::ListStore (GtkListStore) using method insert_with_values
Working sample, but only one column: I can insert data if I have one column:
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]);
However I don't know how to hand over more data if I have more columns. I know that internally it should call gtk_list_store_insert_with_valuesv() so I need probably an array or slice, but if I do something like:
Wanted to pass two elements in array, not working
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]);
it gives me during compilation:
error: the trait bound
[&glib::Value; 2]: glib::IsA<glib::Object>is not satisfied [E0277]
and I don't know how to overcome it.
Not working 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();
}
Cargo.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"

[[bin]]section if your main file issrc/main.rs, that's the conventional spot. - Shepmasterexpected &[>k::ToValue], found &[>k::Value; 2] (expected slice, found array of 2 elements)[E0308]--- Changed to "&array_of_data[..]", compiler complains:expected&[>k::ToValue], found &[>k::Value] (expected trait gtk::ToValue, found structgtk::Value) [E0308]` --- Changed to "&[&array_of_data[..]]", compiler complains:error: the trait bound[>k::Value]: std::marker::Sized` is not satisfied [E0277] note:[>k::Value]does not have a constant size known at compile-time` - wm_obsdgtk::init().expect("Failed to initialize GTK");is shorter and better — it prints the underlying error message. - Shepmaster