Results 1 to 6 of 6

Thread: List of child widgets includes layout

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default List of child widgets includes layout

    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:

    Qt Code:
    1. QWidget* w = (QWidget*)(ui->container->children()->at(0));
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. at(1)
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: List of child widgets includes layout

    Did you come across QStackedWidget

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: List of child widgets includes layout

    Quote Originally Posted by JovianGhost View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: List of child widgets includes layout

    Quote Originally Posted by aamer4yu View Post
    Did you come across QStackedWidget
    Yes, but not what I'm looking for.

    Quote Originally Posted by wysota View Post
    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?

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: List of child widgets includes layout

    Maybe QObject::findChildren() is what you are looking for:
    Qt Code:
    1. ui->container->findChildren<QWidget *>()
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to norobro for this useful post:

    JovianGhost (11th May 2010)

  7. #6
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: List of child widgets includes layout

    Yep, that works, thanks!

Similar Threads

  1. Replies: 5
    Last Post: 18th April 2010, 23:31
  2. How to get the child widgets from a Widget?
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 13:26
  3. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 08:39
  4. setClipPath on child widgets.
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 27th May 2007, 19:12
  5. Global includes with designer custom widgets
    By mab in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 22:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.