[英]How to position an added widget to a layout based on another widget in the same layout?
In my GUI, I would like to add a QComboBox
to a verticalLayout
programmatically based on aa signal triggered by specific action.在我的 GUI 中,我想根据特定操作触发的信号以编程方式将QComboBox
添加到verticalLayout
。 The following code works fine and the widget is added:以下代码工作正常,并添加了小部件:
QComboBox* userOptions = new QComboBox();
ui->verticalLayout_13->addWidget(userOptions);
However, this way the widget is always added to the end of the layout.但是,这种方式总是将小部件添加到布局的末尾。
My question is: how to position the added QComboBox
to the verticalLayout
in alignment to another widget in the same layout ?我的问题是:如何将添加的QComboBox
定位到verticalLayout
以与同一布局中的另一个小部件对齐? (ie: above the "Go" push button for example) (即:例如在“Go”按钮上方)
There doesn't seem to be a way to explicitly insert an item in a layout where you want it to be.似乎没有一种方法可以在您想要的布局中显式插入一个项目。
You have a few choices to achieve that the "hard" way:你有几个选择来实现“困难”的方式:
QLayout::takeAt(int index)
to take all items after the index you want to insert at, insert your item, then insert back the taken items.使用QLayout::takeAt(int index)
获取要插入的索引之后的所有项目,插入您的项目,然后插入取回的项目。QLayout
subclass which supports inserting at a specific index.实现您自己的QLayout
子类,该子类支持在特定索引处插入。 There are several functions you will have to implement.您必须实现几个功能。 EDIT: An omission on my end, as Kuba Ober noted, most of the concrete layout implementations support inserting at a specific index, for example QBoxLayout
derived have insert methods which pass an index as a parameter.编辑:正如 Kuba Ober 指出的那样,我的一个遗漏,大多数具体布局实现都支持在特定索引处插入,例如QBoxLayout
派生的插入方法将索引作为参数传递。
First, iterate a layout to find the index of the reference item you're inserting relative to.首先,迭代布局以查找您插入的参考项的索引。 Then use the concrete layout's specific widget insertion/addition functionality.然后使用具体布局的特定小部件插入/添加功能。
Since you presumably use a QBoxLayout
, you'd use its insertWidget
method to insert a widget.由于您可能使用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;
}
Similar functions can be easily devised for QFormLayout
, QGridLayout
and QStackedLayout
.可以很容易地为QFormLayout
、 QGridLayout
和QStackedLayout
设计类似的功能。
And a test harness:和一个测试工具:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.