Yes. The key observations that enable us to do so are:
A childLayout
can be removed from its parent layout by nulling its parent:
childLayout->setParent(nullptr);
A parentless layout will, upon adding it to a widget or a layout with non-null parentWidget()
, reparent its children.
Thus, we need to add an additional top-level wrapper layout to the form, then unparent the target top level layout from it, and delete the wrapper. The form's structure as reported by dumpObjectTree
is:
QWidget::Form
QVBoxLayout::wrapper
QVBoxLayout::layout
QHBoxLayout::horizontalLayout
QLabel::label
QLabel::label_2
QLabel::label_3
The test case:
// 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();
}
If you wished to factor this functionality out, you'd have a template setupLayout
function that you'd use in place of setupUi
. The testcase above would become:
QWidget parent;
QHBoxLayout layout{&parent};
Ui::Form ui;
layout.addLayout(setupLayout(&ui));
parent.show();
The setupLayout
is:
//*** 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;
}
What if you can't or are unwilling to alter the form's structure? You can use Qt's internals to implement a takeLayout
function that rips out a widget's layout and returns it, unattached to the widget. Note that the private QWidget::takeLayout
is insufficient, as it returns a layout with its topLevel
flag set: such layouts can only be installed directly on widgets, and will fail an assertion when one attempts to add them to a layout (as a sub-layout). Here's how:
#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); }
The form.ui
looks as follows:
<?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>