Results 1 to 4 of 4

Thread: keep child widget centered to parent widget

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows
    Thanks
    17
    Thanked 27 Times in 24 Posts

    Default keep child widget centered to parent widget

    Hello

    What is the best way to center child window inside its parent? Of course, it is possible to subclass parent widget, override resizeEvent, emit resized signal, connect it to the child's slot and finally calculate the position. I'd like to know perhaps there is an easier solution to do that.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: keep child widget centered to parent widget

    Add spacing in the layout manager of the parent widget. See QSpacerItem and the addSpacing() methods of the standard layout classes for details. Better yet, experiment with spacers in QtDesigner to understand how they work.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: keep child widget centered to parent widget

    You mean not just center a child widget on the parent, but to keep it centered as the parent changes geometry right?
    If you are not using Layouts for what ever reason:
    you can also install an event filer on the parent, then you don't need to subclass it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows
    Thanks
    17
    Thanked 27 Times in 24 Posts

    Default Re: keep child widget centered to parent widget

    Ok, thanks. I'd like to clarify that my child widget is a window, it doesn't belong to parent's layout. I use it to create some animation, so this is an overlaying window that should appear on the top of parent window.

    Yes, I want to keep it centered when a parent window is resized.

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. child widget resize to parent widget
    By bobFromAccounting in forum Newbie
    Replies: 10
    Last Post: 11th February 2011, 02:53
  3. Replies: 7
    Last Post: 14th January 2010, 08:47
  4. Replies: 4
    Last Post: 3rd October 2009, 08:19
  5. let parent widget grow with child widget?
    By BeS in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 11:17

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.