PDA

View Full Version : List of child widgets includes layout



JovianGhost
8th May 2010, 02:23
I've got an app that changes around the GUI layout based on user input.

So for part of it, I have a container, which is just a QFrame, which will hold different types of other widgets, but only one at a time. So I need to get the widget from this container, as follows:


QWidget* w = (QWidget*)(ui->container->children()->at(0));

The problem is, w will now point to the LAYOUT object of the container, rather than the widget in the container. In order to get a handle on the widget itself, I have to use
at(1) instead.

This doesn't really sound right to me; I think of the layout is a mechanism for organizing the widgets in another widget, and not as a widget itself. Also, having 0 refer to the layout and 1 refer to the widget makes it seem more like the layout and widget are siblings, which of course isn't right.

Is there a less clunky way to do this, or is this just the nature of the beast?

aamer4yu
8th May 2010, 06:01
Did you come across QStackedWidget :rolleyes:

wysota
8th May 2010, 09:05
This doesn't really sound right to me; I think of the layout is a mechanism for organizing the widgets in another widget, and not as a widget itself. Also, having 0 refer to the layout and 1 refer to the widget makes it seem more like the layout and widget are siblings, which of course isn't right.
children() returns a list of objects, not a list of widgets. The layout is reparented to the widget it is in and widgets inside the layout are reparented to the widget containing the layout so yes, this effectively makes the layout object and child widgets objects siblings.

JovianGhost
11th May 2010, 01:45
Did you come across QStackedWidget :rolleyes:
Yes, but not what I'm looking for. :p


children() returns a list of objects, not a list of widgets. The layout is reparented to the widget it is in and widgets inside the layout are reparented to the widget containing the layout so yes, this effectively makes the layout object and child widgets objects siblings.
Ok, that makes sense, thanks.
Is there a way to get *just* a list of child widgets, or would I have to check each child and see if it's a derivative of QWidget?

norobro
11th May 2010, 02:46
Maybe QObject::findChildren() (http://doc.qt.nokia.com/4.6/qobject.html#findChildren) is what you are looking for:
ui->container->findChildren<QWidget *>()

JovianGhost
11th May 2010, 03:22
Yep, that works, thanks!