how to center the window in the screen ? is there an centreWindow method ?
thanks!
how to center the window in the screen ? is there an centreWindow method ?
thanks!
Check out
QDesktopWidget::availableGeometry()QWidget::frameGeometry()QRect::moveCenter()
so, something like
might work.Qt Code:
// w is your window w->move(r.topLeft());To copy to clipboard, switch view to plain text mode
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
giowck (7th September 2010)
I just want the window to be shown central the first time it is shown. so How ?
thanks anyway!
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.
use this code in constructor
Qt Code:
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());To copy to clipboard, switch view to plain text mode
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);
Bookmarks