英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论