在 Qt 中知道其名称时查找布局

问题描述

this post中,@cgmb 提到:

The parent of a `QWidget` must be a `QWidget`. `QLayout` is not a `QWidget`.

Layouts exist to move and resize the children of a `QWidget`. 
Though you may add child widgets by making calls on the layout,ultimately,their parent will be the `QWidget` that the layout resides on.

例如,像这样使用 findChild() 查找 QLayout 将返回 nullptr

QVBoxLayout *keyLayout = inputFieldGroupBox->findChild<QVBoxLayout*>("keyBoxLayoutCea",Qt::FindChildrenRecursively);

因此,我不得不使用 itemAt() 手动搜索布局:

QVBoxLayout *keyLayout = dynamic_cast<QVBoxLayout*>(firstHoriField->itemAt(3)->layout());

firstHoriFieldkeyLayout所在的布局,inputFieldGroupBoxqgroupbox所在的firstHoriField

是否有任何高效/不易出错的方法可用于查找布局(知道其名称),因为从上面的代码可以看出,itemAt() 方法相当暴力,如果由于某种原因我更改了索引(例如从 3->2),程序将不会运行。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)