how to center a Qt mainform on the screen?
Asked Answered
J

7

7

I've tried these in my mainform's constructor:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - frameGeometry().center());

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - rect().center());

but both put the bottom right corner of the form at about the center of the screen, instead of centering the form. Any ideas?

Jellied answered 6/8, 2010 at 14:51 Comment(0)
F
15

I've tried these in my mainform's constructor

That's likely the problem. You probably don't have valid geometry information at this point because the object isn't visible.

When the object is first constructed, it's essentially positioned at (0,0) with it's expected (width,height), as such:

frame geometry at construction:  QRect(0,0 639x479) 

But, after being shown:

frame geometry rect:  QRect(476,337 968x507) 

Thus, you can't yet rely on your frameGeometry() information.

EDIT: With that said, I presume you can easily move it as desired, but for completeness I'm dropping in Patrice's code which doesn't depend on the frame geometry information:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();

move(center.x() - width() * 0.5, center.y() - height() * 0.5);
Frances answered 6/8, 2010 at 15:28 Comment(2)
I found it works if I call this->resize(width_I_want, height_I_want) before the code to center it. Thanks!Jellied
That's interesting. How does that change the frame geometry?Frances
B
4

The move function (see QWidget doc) takes one QPoint or two int as parameter. This corresponds to the coordinates of the top-left corner of your Widget (relative to its parent; Here OS Desktop). Try:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();

move(center.x()-width*0.5, center.y()-height*0.5);
Bain answered 6/8, 2010 at 15:25 Comment(0)
L
4

availableGeometry() is deprecated.

move(pos() + (QGuiApplication::primaryScreen()->geometry().center() - geometry().center()));
Liege answered 4/9, 2018 at 14:8 Comment(0)
M
2
#include <QStyle>
#include <QDesktopWidget>

window->setGeometry(
    QStyle::alignedRect(
        Qt::LeftToRight,
        Qt::AlignCenter,
        window->size(),
        qApp->desktop()->availableGeometry()
    )
);

https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

Malacostracan answered 20/10, 2017 at 2:16 Comment(0)
F
1

move(QGuiApplication::primaryScreen()->geometry().center() - rect().center());

Finical answered 18/6, 2021 at 13:14 Comment(0)
C
0

PyQT Python Version

# Center Window
desktopRect = QApplication.desktop().availableGeometry(self.window)
center = desktopRect.center();
self.window.move(center.x()-self.window.width()  * 0.5,
                 center.y()-self.window.height() * 0.5);   
Counterpressure answered 21/8, 2018 at 15:8 Comment(0)
J
-1

Another solution, assuming the window in question is 800×800:

QRect rec = QApplication::desktop()->availableGeometry();
move(QPoint((rec.width()-800)/2, (rec.height()-800)/2));
Jackdaw answered 19/9, 2016 at 21:25 Comment(1)
I'm not a fan of assuming window sizes, especially in the age of high-DPI displays and ever changing landscape of mobile devices.Counterpressure

© 2022 - 2024 — McMap. All rights reserved.