我们如何将Rust字符串转换为gtk::type::String?

3

我正在尝试创建一个ComboBox,特别是它的模型:

let type_in_col = &[gtk::Type::String];
let list_model = ListStore::new(type_in_col);
list_model.insert_with_values(None, &[0], &[""]);
list_model.insert_with_values(None, &[0], &["h"]);
list_model.insert_with_values(None, &[0], &["H"]);
list_model.insert_with_values(None, &[0], &["W"]);
list_model.insert_with_values(None, &[0], &["S"]);

这段代码给我报错了:

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
--> src\widgets\daywidget.rs:36:1
   |
36 | #[widget]
   | ^^^^^^^^^ `str` does not have a constant size known at compile-time
   |
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: required for the cast to the object type `gtk::ToValue`

由于我正在使用Relm,所以错误并不是非常精确。


3
猜测一下,您可能需要使用String而不是str。您可以使用"example".to_string()str转换为String - Wesley Wiser
1
我找到了一个例子,它执行的是 list_model.insert_with_values(None, &[0], &[&("S".to_value()) as &ToValue]); - Geob-o-matic
1
你应该把那个作为一个答案添加进去! - Wesley Wiser
1个回答

2
您应该使用以下内容:
let type_in_col = &[gtk::Type::String];
let list_model = ListStore::new(type_in_col);
list_model.insert_with_values(None, &[0], &[&""]);
list_model.insert_with_values(None, &[0], &[&"h"]);
list_model.insert_with_values(None, &[0], &[&"H"]);
list_model.insert_with_values(None, &[0], &[&"W"]);
list_model.insert_with_values(None, &[0], &[&"S"]);

因为 SetValue 的实现是针对 &T where T: ?Sized

你不能将 &str 转换为 &ToValue:请看 这里 了解原因。


此函数需要4个参数,但只提供了3个参数(期望4个参数)[E0061] [4次] 在“None”的位置。 - Zelphir Kaltstahl
1
@Zelphir:你可能正在使用TreeStore而不是ListStore。前者还需要指定一个父项,可以是None - antoyo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接