如何将添加的小部件定位到基于同一布局中的另一个小部件的布局中
How to position an added widget to a layout based on another widget in the same layout?
在我的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;
}
可以容易地为QFormLayout
、QGridLayout
和QStackedLayout
设计类似的功能。
还有一个测试线束:
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();
}
相关文章:
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- 调整布局上的 QGraphicsView 小部件的大小
- 将小部件添加到布局后,QStylesheet 不起作用
- Qt小部件将子布局堆叠在彼此之上
- Qt:自定义小部件的多个布局
- 如何在布局中与其他小部件一起安排Qcamera
- QT设计器中的自定义小部件在框架中具有预先存在的布局
- 重叠小部件的 QT 自定义布局示例无法编译
- 重新设置小部件pyqt的布局
- 在QT中使用两个不同的布局中使用相同的小部件
- 如何在布局QT上销毁小部件
- 为什么接收事件的多个小部件需要布局
- 如果有可用的位置,如何在布局中添加小部件?否则不添加
- C++ 创建小部件数组
- 如何将添加的小部件定位到基于同一布局中的另一个小部件的布局中
- Qt小部件布局重叠
- 动态地将自定义 qt 小部件添加到框架中,而无需每次都更改布局
- Qt动态创建布局并向布局添加小部件
- Qt小部件布局背景
- Qt小部件布局添加事件