Search code examples
qtqt4qwidgetqt4.8

QWidget get geometry when maximized


QWidget provides functions for getting the geometry of the widget:

QWidget::frameGeometry()
QWidget::geometry()
QWidget::normalGeometry()

However, when the QWidget is maximized (QWidget::isMaximized() returns true), the rectangle provided by the geometry functions has screen coordinates (QRect::x(), QRect::y()) that are the same for when the QWidget is not maximized while the size of the window is correct. Is there a way to get the screen coordinates of the maximized window? Note that using (0,0) is incorrect because there may be a panel located at the top of the screen.

I am using Qt 4.8 on CentOS 6.3, if that matters.


Solution

  • The solution comes from looking at the QWidget's state (isMaximized, isFullscreen) and then getting geometry from the QDesktopWidget

    QWidget *widget = ...
    QRect geom;
    if (widget->isMaximized()) {
        geom = QApplication::desktop()->availableGeometry()
    } else if (widget->isFullScreen()) {
        geom = QApplication::desktop()->screenGeometry()
    } else {
        geom = widget->frameGeometry()
    }