如何将添加的小部件定位到基于同一布局中的另一个小部件的布局中

How to position an added widget to a layout based on another widget in the same layout?

本文关键字:小部 布局 另一个 于同一 定位 添加      更新时间:2023-10-16

在我的GUI中,我想根据特定操作触发的信号以编程方式将QComboBox添加到verticalLayout。以下代码运行良好,并添加了小部件:

QComboBox* userOptions = new QComboBox();
ui->verticalLayout_13->addWidget(userOptions);

但是,通过这种方式,小部件总是被添加到布局的末尾。

我的问题是:如何将添加的QComboBox定位到verticalLayout,使其与同一布局中的另一个小部件对齐?(例如:在"Go"按钮上方)

似乎没有一种方法可以在布局中明确地将项目插入到您想要的位置。

你有几个选择来实现这一"艰难"的方式:

  • 使用QLayout::takeAt(int index)获取要插入的索引之后的所有项目,插入您的项目,然后再插入已获取的项目
  • 创建一个占位符小部件,可以用来在布局中保留索引,然后不在布局中插入项目,而是在占位符小部件内嵌套的布局中插入。如果没有项目,占位符小部件就不会占用空间,并且会扩展以容纳放入其中的任何内容
  • 实现您自己的QLayout子类,该子类支持在特定索引处插入。您必须实现几个功能

编辑:正如Kuba Ober所指出的,我的一个遗漏是,大多数具体布局实现都支持在特定索引处插入,例如QBoxLayout派生的插入方法将索引作为参数传递。

首先,迭代一个布局,找到要插入的引用项的索引。然后使用具体布局的特定小部件插入/添加功能。

由于您可能使用QBoxLayout,因此您将使用其insertWidget方法来插入小部件。

// https://github.com/KubaO/stackoverflown/tree/master/questions/insert-widget-36746949
#include <QtWidgets>
namespace SO { enum InsertPosition { InsertBefore, InsertAfter }; }
bool insertWidget(QBoxLayout * layout, QWidget * reference, QWidget * widget,
                  SO::InsertPosition pos = SO::InsertBefore, int stretch = 0,
                  Qt::Alignment alignment = 0) {
   int index = -1;
   for (int i = 0; i < layout->count(); ++i)
      if (layout->itemAt(i)->widget() == reference) {
         index = i;
         break;
      }
   if (index < 0) return false;
   if (pos == SO::InsertAfter) index++;
   layout->insertWidget(index, widget, stretch, alignment);
   return true;
}

可以容易地为QFormLayoutQGridLayoutQStackedLayout设计类似的功能。

还有一个测试线束:

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget w;
   QVBoxLayout l{&w};
   QLabel first{"First"};
   QLabel second{"Second"};
   l.addWidget(&first);
   l.addWidget(&second);
   insertWidget(&l, &first, new QLabel{"Before First"}, SO::InsertBefore);
   insertWidget(&l, &second, new QLabel{"After Second"}, SO::InsertAfter);
   w.show();
   return app.exec();
}