在 PyQT 中获取布局的小部件

编程入门 行业动态 更新时间:2024-10-20 20:40:04
本文介绍了在 PyQT 中获取布局的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 QVBoxLayout,我通过 addWidget() 添加了一些小部件.我现在需要删除这些小部件,看来我需要使用 removeWidget()(它接收要删除的小部件)来做到这一点.

I have a QVBoxLayout that I've added a few widgets to, via addWidget(). I need to now delete those widgets, and it seems I need to use removeWidget() (which takes in a widget to be removed) to do that.

我认为在布局上调用 children() 或 findChildren(QWidget) 会返回我添加到其中的小部件列表;不过,我在调试器中,只是收到空列表.

I thought that calling children() or findChildren(QWidget) on my layout would return a list of the widgets I've added into it; I'm in the debugger, though, and am just receiving empty lists.

我是不是误会了什么?我上周刚刚开始使用 PyQT,主要是通过 API 文档的反复试验来学习.

Am I terribly misunderstanding something? I've just started doing PyQT this last week and have mostly been learning through trial and error with the API docs.

推荐答案

这很奇怪.我的理解是通过 addWidget 添加小部件会将所有权转移到布局,因此调用 children() 应该可以工作.

That's odd. My understanding is that adding widgets via addWidget transfers ownership to the layout so calling children() ought to work.

但是,作为替代方案,您可以使用 count() 和 itemAt(int) 来循环布局项以提供 QLayoutItemremoveItem(QLayoutItem*).

However, as an alternative you could loop over the layout items by using count() and itemAt(int) to supply a QLayoutItem to removeItem(QLayoutItem*).

我刚刚用一个直接的 C++ 测试应用程序尝试了 addWidget.并且它不会将 QObject 所有权转移到布局,因此 children() 确实是一个空列表.docs 明确说明所有权已转移...

I've just tried addWidget with a straight C++ test app. and it doesn't transfer QObject ownership to the layout so children() is indeed an empty list. The docs clearly say that ownership is transferred though...

编辑 2:

好的,它看起来好像将所有权转移给具有该布局的小部件(这不是文档所说的).这使得 QObject 层次结构中布局本身的布局兄弟中的项目!因此更容易坚持使用 count 和 itemAt.

Okay, it looks as though it transfers ownership to the widget that has that layout (which is not what the docs said). That makes the items in the layout siblings of the layout itself in the QObject hierarchy! It's therefore easier to stick with count and itemAt.

更多推荐

在 PyQT 中获取布局的小部件

本文发布于:2023-11-27 03:44:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   布局   PyQT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!