PDA

View Full Version : center application on screen in multiheaded system



jiggersplat
6th April 2012, 18:13
i'm trying to center my app on whatever screen it is currently on, on a multiheaded system. i get how to center it, but the problem i'm having is i am getting 0x0 for the dimension of my program. i call resize(0,0) before center it to compact the program to it's minimum size. w.geometry().width()/height() and w.size().width()/height() both return zero. is there some other way i should be compacting the main window or some other way to get *actual* dimensions? if i skip the call to resize() i get 640x480 as the size of the window which is not accurate either.




// w is a QMainWindow

// compact the program
w.resize(0,0);

// center it
QDesktopWidget *desktop = QApplication::desktop();
int screenNumber = desktop->screenNumber(&w);
int screenWidth = desktop->screenGeometry(desktop->screen(screenNumber)).width();
int screenHeight = desktop->screenGeometry(desktop->screen(screenNumber)).height();
int x = (screenWidth - w.geometry().width()) / 2;
int y = (screenHeight - w.geometry().height()) / 2;
w.move(x, y - 50);

ChrisW67
7th April 2012, 00:22
If you are running the code above in the constructor of your widget then there is probably no meaningful size: try looking after you show() it. It is also possible that the window can resize to zero if nothing inside it constrains the minimum size.

jiggersplat
8th April 2012, 17:56
yep... show() first did the trick.