Getting parent layout in Qt

quick question. Is there a (easy) way to get the widget's parent layout in Qt?

PS: QObject :: parent () will not work for logical reasons.

EDIT: I'm sure the widget has a parent layout, because I added it to the layout earlier in the code. Now I have many other layouts in the window, and although I can track them, I just want to know if there is an easy and clean way to get the parent layout.

EDIT2: Sorry, โ€œeasy and cleanโ€ was probably not the best way. I meant using the Qt API.

EDIT3: I add the widget to the layout as follows:

QHBoxLayout * layout = new QHBoxLayout;

layout-> addWidget (button);

+7
c ++ qt layout parents
source share
7 answers

After some research, I found a "partial" solution to the problem.

If you create a layout and control widgets with it, you can get this layout later in the code using the dynamic properties of Qt. Now, in order to use QWidget :: setProperty (), the object you are about to store must be a registered meta type. A pointer to a QHBoxLayout is not a registered meta type, but there are two workarounds. The simplest workaround is to register the object by adding this anywhere in your code:

Q_DECLARE_METATYPE(QHBoxLayout*) 

The second workaround is to wrap the object:

 struct Layout { QHBoxLayout* layout; }; Q_DECLARE_METATYPE(Layout) 

Once an object is a registered meta-type, you can save it as follows:

 QHBoxLayout* layout = new QHBoxLayout; QWidget* widget = new QWidget; widget->setProperty("managingLayout", QVariant::fromValue(layout)); layout->addWidget(widget); 

Or, if you used the second workaround:

 QHBoxLayout* layout = new QHBoxLayout; QWidget* widget = new QWidget; Layout l; l.layout = layout; widget->setProperty("managingLayout", QVariant::fromValue(l)); layout->addWidget(widget); 

Later, when you need to get the layout, you can get it as follows:

 QHBoxLayout* layout = widget->property("managingLayout").value<QHBoxLayout*>(); 

Or like this:

 Layout l = widget->property("managingLayout").value<Layout>(); QHBoxLayout* layout = l.layout; 

This approach is applicable only when creating a layout. If you have not created the layout and installed it, then there is no easy way to get it later. You will also need to track the layout and update the manageLayout property when necessary.

+1
source share

(Updated answer)

I think this is impossible. Since the widget can be technically contained in several layouts (horizontal layout, which, for example, is aligned inside a vertical layout).

Just remember that the QWidget parent does not change if it is aligned in the layout.

You may need to track this yourself.

+5
source share

use widget.parent (). layout () and search brute force (included recursion) is my only advice. Perhaps you can search for "name".

+1
source share

SOLVE! Usage: QLayout * parentLayout = findParentLayout (added by Widget)

 QLayout* findParentLayout(QWidget* w) { if (w->parentWidget() != nullptr) if (w->parentWidget()->layout() != nullptr) return layout = findParentLayout(w, w->parentWidget()->layout()); return nullptr; } QLayout* findParentLayout(QWidget* w, QLayout* topLevelLayout) { for (QObject* qo: topLevelLayout->children()) { QLayout* layout = qobject_cast<QLayout*>(qo); if (layout != nullptr) { if (layout->indexOf(w) > -1) return layout; else if (!layout->children().isEmpty()) { layout = findParentLayout(w, layout); if (layout != nullptr) return layout; } } } return nullptr; }; 
+1
source share

Just use:

 QHBoxLayout* parentLayout = button->parentWidget()->layout(); 

I assume that button is a child of the widget that contains the layout containing the button . button->parentWidget() returns a pointer to the widget of the parent of the button and ->layout() returns a pointer to the layout of the parent.

+1
source share

Have you tried this? Remember to check for NULL.

 QLayout *parent_layout = qobject_cast< QLayout* >( parent() ); 

If parent_layout is NULL, then the parent widget is not a layout.

-3
source share

Have you tried QWidget::layout() ?

-3
source share

All Articles