Results 1 to 8 of 8

Thread: Is it possible to find a widget's Layout which add it?

  1. #1
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Is it possible to find a widget's Layout which add it?

    For example,
    Qt Code:
    1. QHBoxLayout mainHLayout;
    2.  
    3. mainHLayout.addWidget(&btn);
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by HiJack; 29th April 2010 at 10:43.

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Is it possible to find a widget's Layout which add it?

    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.
    Last edited by Kumosan; 29th April 2010 at 10:56. Reason: updated contents

  3. #3
    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: Is it possible to find a widget's Layout which add it?


  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Is it possible to find a widget's Layout which add it?

    Quote Originally Posted by aamer4yu View Post
    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.
    Last edited by Kumosan; 29th April 2010 at 11:18. Reason: spelling corrections

  5. #5
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Is it possible to find a widget's Layout which add it?

    Quote Originally Posted by Kumosan View Post
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Is it possible to find a widget's Layout which add it?

    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.

  7. The following user says thank you to tbscope for this useful post:

    HiJack (30th April 2010)

  8. #7
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Is it possible to find a widget's Layout which add it?

    Quote Originally Posted by tbscope View Post
    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.

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

    Default Re: Is it possible to find a widget's Layout which add it?

    How about:
    Qt Code:
    1. btn.parentWidget()->layout()
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. How to change a widget to other widget in layout?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2010, 10:55
  2. Replies: 1
    Last Post: 4th September 2009, 09:58
  3. Layout (widget overlay)
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2009, 23:54
  4. how to find a child widget?
    By TheRonin in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 10:30
  5. Cannot find line or rectangle widget
    By Kapil in forum Newbie
    Replies: 3
    Last Post: 23rd February 2006, 11:01

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.