从 ListBox(gtk4)中获取子项

huangapple go评论52阅读模式
英文:

Get children from ListBox (gtk4)

问题

在使用gtk3时,我可以通过以下方式访问ListBox的子元素:

let list_box = ListBox::new();
let label = Label::new(Some("Label 1"));

list_box.append(&label);

for row in list_box.children() {
    list_box.remove(&row);
}

gtk4中,似乎不再存在children()方法:“在gtk4::ListBox结构体中找不到名为children的方法”。

我查看了gtk4文档中的ListBox,但未找到与访问ListBox的子元素相关的信息。

如何在gtk4中访问ListBox的子元素?

英文:

When I was using gtk3 I was able to access a ListBox's children this way:

let list_box = ListBox::new(); 
let label = Label::new(Some("Label 1")); 

list_box.append(&label);

for row in list_box.children() {
    list_box.remove(&row);
}

In gtk4, it seems like children() doesn't exist anymore: "no method named `children` found for struct `gtk4::ListBox`".

I checked ListBox in the gtk4 docs, but I couldn't find anything related to accessing a ListBox's children.

How to access a ListBox's children in gtk4?

答案1

得分: 2

observe_children,但正如其名称所示,你不应在使用它时修改容器。更具体地说,一旦修改了底层容器,就不能继续使用它的迭代器。

对于你的用例,你可以像这样迭代并移除子元素,使用 first_child/last_child

while let Some(row) = list_box.last_child() {
    list_box.remove(&row);
}
英文:

There is observe_children, but like it's name suggests you should not modify the container while using it. Or more specifically you can't keep using it's iterator once you modify the underlying container.

For your usecase you can iterate and remove children using first_child/last_child like this:

while let Some(row) = list_box.last_child() {
    list_box.remove(&row);
}

huangapple
  • 本文由 发表于 2023年2月14日 01:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定