【问题标题】:How do we convert a Rust string to a gtk::type::String?我们如何将 Rust 字符串转换为 gtk::type::String?
【发布时间】:2017-06-02 06:01:02
【问题描述】:

我正在尝试创建一个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)

【问题讨论】:

  • 只是猜测,但您可能需要String 而不是str。您可以使用"example".to_string()str 转换为String
  • 我设法找到了一个例子,它正在做list_model.insert_with_values(None, &[0], &[&("S".to_value()) as &ToValue]);
  • 您应该将其添加为答案!

标签: rust gtk-rs


【解决方案1】:

您想改用以下内容:

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:请参阅here 了解原因。

【讨论】:

  • this function takes 4 parameters but 3 parameters were supplied (expected 4 parameters) [E0061] [4 times]Nones
  • @Zelphir:您可能使用的是TreeStore 而不是ListStore。前者还需要指定一个父级,可以是None
猜你喜欢
  • 2017-06-05
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 2019-12-12
  • 2020-11-22
相关资源
最近更新 更多