View Full Version : keep child widget centered to parent widget

22nd August 2011, 14:28

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.

22nd August 2011, 14:56
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.

22nd August 2011, 14:57
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.

22nd August 2011, 16:18
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.