PDA

View Full Version : how to show window in the center of the screen?



lovelypp
12th July 2008, 06:25
how to center the window in the screen ? is there an centreWindow method ?
thanks!

caduel
12th July 2008, 10:44
Check out
QDesktopWidget::availableGeometry()
QWidget::frameGeometry()
QRect::moveCenter()

so, something like

// w is your window
QRect r = w->frameGeometry();
r.moveCenter(QDesktopWidget::availableGeometry());
w->move(r.topLeft());
might work.

You have to take care about when you call that, though.
Things like frameGeometry() etc return incorrect values if called "too soon" (if the widget is not yet shown).
You might have to execute that code in reaction to an internal QShowEvent.

HTH

lovelypp
12th July 2008, 16:33
I just want the window to be shown central the first time it is shown. so How ?
thanks anyway!

LarryP
15th July 2011, 23:38
In Visual Studio when you design a form, it has the option to center it a few different ways. Designer should also have that option in the properties section, but nothing even remotely related is there for window placement. However, I'm certainly not going back to VS anytime soon if ever. :)

hamidarr
17th July 2011, 10:59
use this code in constructor

QDesktopWidget *desktop = QApplication::desktop();

int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;

screenWidth = desktop->width();
screenHeight = desktop->height();

windowSize = size();
width = windowSize.width();
height = windowSize.height();


x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;



move ( x, y );

setFixedSize(windowSize.width(), windowSize.height());

jeux
18th March 2014, 22:43
include QDesktopWidget:
#include <QDesktopWidget>

after that let's say your window is "w" (MainWindow w;)
w.move(d.geometry().center().x() - w.width() / 2, d.geometry().center().y() - w.height() / 2);