【问题标题】:How can I enumerate layouts inside layout?如何枚举布局内的布局?
【发布时间】:2015-06-23 10:59:43
【问题描述】:

我可以枚举布局内的小部件,但我需要枚举布局内的小部件...

我正在尝试:

  while (QHBoxLayout* currentLayout = m_Layout->findChild<QHBoxLayout*>()) {
    while (QCheckBox* currentCheckbox = currentLayout->findChild<QCheckBox*>()) {
      if (currentCheckbox->isChecked()) {

      }
    }
  }

但是这段代码卡住了......我想这可能是因为我找不到 QHBoxLayout,还有其他可能的方法可以枚举布局中的布局吗?

谢谢

【问题讨论】:

  • QLayout::itemAt() 可能会有所帮助。
  • 一个问题。 while (QCheckBox* currentCheckbox = currentLayout-&gt;findChild&lt;QCheckBox*&gt;()) 是否返回小部件列表?如果有多个直系祖先,则未定义将返回哪一个。在这种情况下,应该使用findChildren()

标签: c++ qt qtwidgets


【解决方案1】:
for (int i = 0; i < layout->count(); ++i) {
    QLayoutItem *item = layout->itemAt(i);
    if (item->widget()) {
        processWidget(item->widget());
    } else if (item->layout()) {
        processLayout(item->layout());
    }
}

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多