对 QObject 子对象进行排序

发布于 2024-10-06 06:12:06 字数 121 浏览 0 评论 0原文

我注意到 QObject::children() 方法返回一个对 QObjectList 的 const 引用,其中包含 qobject 的子对象。放弃常量并使用 qSort 对列表进行排序是否安全?

谢谢 安东

i noticed that QObject::children() method return a const reference to QObjectList, that contains the children of the qobject. Will it be safe to cast away the constness and sort the list with qSort?

Thanks
Anton

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-10-13 06:12:07

或许。但为什么要冒险呢?难道你不能只复制QObjectList并对本地副本进行排序吗?

Maybe. But why risk it? Can't you just copy the QObjectList and sort your local copy?

季末如歌 2024-10-13 06:12:06

不,你不应该这样做。这是 QObject::children() 的定义:

inline const QObjectList &children() const { return d_ptr->children; }

这意味着您可以直接获取内部对象列表的引用。
文档说:

请注意,列表顺序会在以下情况发生变化:
QWidget 孩子被抚养或
降低了。一个被提升的小部件
成为列表中的最后一个对象,
一个降低的小部件变成
列表中的第一个对象。

弄乱这个列表看起来是个坏主意。

No, you should not do this. This is the definition of QObject::children():

inline const QObjectList &children() const { return d_ptr->children; }

This means you directly get the reference of the internal objectlist.
And the documentation says:

Note that the list order changes when
QWidget children are raised or
lowered. A widget that is raised
becomes the last object in the list,
and a widget that is lowered becomes
the first object in the list.

It looks like a bad idea to mess with this list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文