是的。使我们能够这样做的关键观察结果是:
AchildLayout
可以通过将其父级设为空来从其父级布局中删除:
childLayout->setParent(nullptr);
无父布局将在将其添加到小部件或具有 non-null 的布局时,重新设置parentWidget()
其子级。
因此,我们需要向表单添加一个额外的顶级包装布局,然后从中取消目标顶级布局的父级,并删除该包装。报告的表单结构为dumpObjectTree
:
QWidget::Form
QVBoxLayout::wrapper
QVBoxLayout::layout
QHBoxLayout::horizontalLayout
QLabel::label
QLabel::label_2
QLabel::label_3
测试用例:
// https://github.com/KubaO/stackoverflown/tree/master/questions/layout-take-40497358
#include <QtWidgets>
#include "ui_form.h"
QLayout * takeLayout(QWidget *);
int main(int argc, char ** argv)
{
QApplication app{argc, argv};
QWidget parent;
QHBoxLayout layout{&parent};
Ui::Form ui;
{
QWidget w;
ui.setupUi(&w);
w.dumpObjectTree();
if (true) {
ui.layout->setParent(nullptr);
delete ui.wrapper;
layout.addLayout(ui.layout);
} else {
layout.addLayout(takeLayout(&w));
}
}
parent.show();
return app.exec();
}
如果您希望排除此功能,您将拥有一个模板setupLayout
函数来代替setupUi
. 上面的测试用例将变为:
QWidget parent;
QHBoxLayout layout{&parent};
Ui::Form ui;
layout.addLayout(setupLayout(&ui));
parent.show();
是setupLayout
:
//*** Interface
QLayout * setupLayout_impl(void * ui, void(*setupUi)(void * ui, QWidget * widget));
// Extracts the top layout from a Ui class generated by uic. The top layout must
// be enclosed in a wrapper layout.
template <typename Ui> QLayout * setupLayout(Ui * ui) {
struct Helper {
static void setupUi(void * ui, QWidget * widget) {
reinterpret_cast<Ui*>(ui)->setupUi(widget);
}
};
return setupLayout_impl(static_cast<void*>(ui), &Helper::setupUi);
}
//*** Implementation
static void unparentWidgets(QLayout * layout) {
const int n = layout->count();
for (int i = 0; i < n; ++i) {
QLayoutItem * item = layout->itemAt(i);
if (item->widget()) item->widget()->setParent(0);
else if (item->layout()) unparentWidgets(item->layout());
}
}
QLayout * setupLayout_impl(void * ui, void(*setupUi)(void * ui, QWidget * widget))
{
QWidget widget;
setupUi(ui, &widget);
QLayout * wrapperLayout = widget.layout();
Q_ASSERT(wrapperLayout);
QObjectList const wrapperChildren = wrapperLayout->children();
Q_ASSERT(wrapperChildren.size() == 1);
QLayout * topLayout = qobject_cast<QLayout*>(wrapperChildren.first());
Q_ASSERT(topLayout);
topLayout->setParent(0);
delete wrapperLayout;
unparentWidgets(topLayout);
Q_ASSERT(widget.findChildren<QObject*>().isEmpty());
return topLayout;
}
如果您不能或不愿意更改表单的结构怎么办?您可以使用 Qt 的内部实现一个takeLayout
函数,该函数将一个小部件的布局剥离并返回,不附加到小部件上。请注意,私有QWidget::takeLayout
是不够的,因为它返回一个带有其topLevel
标志集的布局:此类布局只能直接安装在小部件上,并且当尝试将它们添加到布局(作为子布局)时将失败断言。就是这样:
#include <private/qwidget_p.h>
#include <private/qlayout_p.h>
class WidgetHelper : private QWidget {
struct LayoutHelper : private QLayout {
static void resetTopLevel(QLayout * l) {
auto d = static_cast<QLayoutPrivate*>(static_cast<LayoutHelper*>(l)->d_ptr.data());
d->topLevel = false;
}
};
public:
static QLayout * takeLayout(QWidget * w) {
auto d = static_cast<QWidgetPrivate*>(static_cast<WidgetHelper*>(w)->d_ptr.data());
auto l = w->layout();
if (!l) return nullptr;
d->layout = 0;
l->setParent(nullptr);
LayoutHelper::resetTopLevel(l);
return l;
}
};
QLayout * takeLayout(QWidget * w) { return WidgetHelper::takeLayout(w); }
form.ui
外观如下:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<layout class="QVBoxLayout" name="wrapper">
<item>
<layout class="QVBoxLayout" name="layout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Top</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Left</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Right</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</ui>