[Solved] QMainWindow to the center of the screen
-
Hi everyone,
i have a simple widget project and i want my mainwindow to be appear on the center of the user's screen.
is this possible ?Thanks in advance
-
Yes, it is possible. Not by using a single magic function, but pretty simple nonetheless. You need to get the available geometry ("qApp->desktop()":http://qt-project.org/doc/qt-4.8/qapplication.html#desktop) then use availableGeometry() from resulting QDesktopWidget. It returns the part of the screen that the app can be displayed on. Then it's just a bit of math: find the central point of the screen and your application's window, and align those. Just remember that in widgets, x() and y() correspond to coords including the window frame.
-
[quote author="sierdzio" date="1350891105"]Yes, it is possible. Not by using a single magic function, but pretty simple nonetheless. You need to get the available geometry ("qApp->desktop()":http://qt-project.org/doc/qt-4.8/qapplication.html#desktop) then use availableGeometry() from resulting QDesktopWidget. It returns the part of the screen that the app can be displayed on. Then it's just a bit of math: find the central point of the screen and your application's window, and align those. Just remember that in widgets, x() and y() correspond to coords including the window frame.[/quote]
Thank you ! i did it with the code below
@
MyClass w;
int width = w.frameGeometry().width();
int height = w.frameGeometry().height();QDesktopWidget wid; int screenWidth = wid.screen()->width(); int screenHeight = wid.screen()->height(); w.setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height); w.show();
@
-
Perfect! I'm pleased to read this. If you think it's solved, please add "[Solved]" to the beginning of the post's title.
-
It can also be done like this,
@int main(int argc, char *argv[])
{int W = 250;
int H = 250;int screenWidth;
int screenHeight;int x, y;
QApplication app(argc, argv);
QWidget window;
QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width();
screenHeight = desktop->height();x = (screenWidth - W) / 2;
y = (screenHeight - H) / 2;window.resize(W, H);
window.move( x, y );
window.setWindowTitle("Center");
window.show();return app.exec();
}@ -
[quote author="XGuy" date="1376914979"]simply call:
void QWidget::showNormal () [slot]
@this->showNoraml()@
[/quote]did you read the docs on this method?!
Calling this method has not the effect the thread author asked for. -
[quote author="raven-worx" date="1376919121"][quote author="XGuy" date="1376914979"]simply call:
void QWidget::showNormal () [slot]
@this->showNoraml()@
[/quote]did you read the docs on this method?!
Calling this method has not the effect the thread author asked for.
[/quote]yes you are right. sorry for that! but in my case this function maximizes my application and shows it on the center of the screen.
although i'm sure this approach would not work on multi-screen PCs. -
[quote author="XGuy" date="1376971298"]but in my case this function maximizes my application and shows it on the center of the screen.[/quote]
maybe because it was centered before you maximized it.