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
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);
// 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);
To copy to clipboard, switch view to plain text mode
Bookmarks