PDA

View Full Version : Centering child window in parent



squidge
10th August 2011, 12:23
When opening a child window, I want the default behavior to open the window in the center of the parent window. Research suggests that I would have to calculate the childs position based on the parent geometry, but I'm sure there must be an easier way (and I'm not sure how this would work when the parent is on a different monitor to which the child opens on by default)

Dong Back Kim
11th August 2011, 02:11
(as a novice) I wonder whether Qt provides an interface to deal with multiple monitors and their properties (i.e. resolution and geometric relationships). If there is then it must not be that hard I believe.

mattc
11th August 2011, 17:22
If the child window is a QDialog, then it will be centered on the parent (exact wording from the docs is "it is centered on top of the parent's top-level widget")

For other windows, I am using the following code:


move(
parentWidget()->window()->frameGeometry().topLeft() +
parentWidget()->window()->rect().center() -
rect().center());