PDA

View Full Version : Is it possible to find a widget's Layout which add it?



HiJack
29th April 2010, 11:15
For example,


QHBoxLayout mainHLayout;
QPushButton btn;

mainHLayout.addWidget(&btn);

QLayout* lo = btn.???();
After adding a widget to the layout, how can I find out the layout , it seems that there's no such a member in QWidget?

Kumosan
29th April 2010, 11:50
I really doubt that you can do it. It it just not the business of widgets to know how and by whom they are layouted.

There might be a dirty way. When you add a widget to a layout it gets reparented. You might want to check the parent.
But I have no idea what happens when you have complicated layout hierachies.

aamer4yu
29th April 2010, 12:12
QWidget::layout

Kumosan
29th April 2010, 12:18
QWidget::layout

Is this really correct?
According to assitant:

The layout manager sets the geometry of the widget's children that have been added to the layout.
I might misinterpret this, but ii is not the layout which layouts this widget, but the layout, which is contained in this widget, which layouts its children.

HiJack
30th April 2010, 04:03
I really doubt that you can do it. It it just not the business of widgets to know how and by whom they are layouted.

There might be a dirty way. When you add a widget to a layout it gets reparented. You might want to check the parent.
But I have no idea what happens when you have complicated layout hierachies.

yes, i misunderstood this. For me, it is amazing that two layouts can add same child widget, though only one position which added could be shown at the same time. In this case, it make no sense to check the only layout. we can test that, it paints ambiguously when i resize the main window from left to right. But i 'm still confused. there got to be some api at least? A list contain the layouts? or something else.

tbscope
30th April 2010, 07:08
There are a couple of ways to do this:

1. Use int QLayout::indexOf ( QWidget * widget ) const
2. Subclass your widgets and add a properrty to hold the containing layout. You need to create your widgets in code or at least, in the constructor of your window set them in the widget using your custom set function.
3. If you do not want to subclass, create a list of pairs, example: QList<QPair<QWidget *, QLayout *> > widgetlayoutlist; and add the widget and the containing layout pair to the list.

HiJack
30th April 2010, 07:23
There are a couple of ways to do this:

1. Use int QLayout::indexOf ( QWidget * widget ) const
2. Subclass your widgets and add a properrty to hold the containing layout. You need to create your widgets in code or at least, in the constructor of your window set them in the widget using your custom set function.
3. If you do not want to subclass, create a list of pairs, example: QList<QPair<QWidget *, QLayout *> > widgetlayoutlist; and add the widget and the containing layout pair to the list.

Actually i managed to it through 1 & 2, previously i just thought it maybe has some qt api could make it.
Anyway, thank u all.:)

wysota
30th April 2010, 10:01
How about:

btn.parentWidget()->layout()