QWidget::geometry() vs. QWidget::frameGeometry()
Asked Answered
T

5

6

Although Qt's docs indicate that these two functions are different (the first doesn't include the frame) no matter what widget I choose - including the main window of my application - someWidget->frameGeometry().height() always returns the same value as someWidget->geometry.height().

What am I missing here?

Tattered answered 26/1, 2013 at 18:23 Comment(0)
J
8

I think, you don't give enough time to widget to be painted. There is little example:

#include <QApplication>
#include <QMainWindow>
#include <QDebug>

class MainWindow : public QMainWindow
{
public:
    MainWindow() {
        startTimer(500);
    }

    void timerEvent(QTimerEvent *e) {
        // Here values are different
        qDebug() << geometry().height() << frameGeometry().height();
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWin;
    mainWin.show();

    // Here values are equals
    qDebug() << mainWin.geometry().height() << mainWin.frameGeometry().height();

    return app.exec();
}

First debug output will produce the same values for geometry and frameGeometry, but the second (in timerEvent) will produce different.

Jerk answered 26/1, 2013 at 18:42 Comment(2)
Thanks for the illustration -- very helpful.Tattered
@Planarian but I don't know, how long time exactly it takes to be completely painted :(Jerk
I
1

The QWidget class cannot have a frame. For example, QWidget doesn't have a frame, but QFrame has a frame.

Ingleside answered 26/1, 2013 at 18:41 Comment(1)
But don't plenty of QWidgets have them? QMdiSubWindow, for instance, doesn't inherit QFrameTattered
S
1

if QWidget is toplevel window then you can see borders and title bar around it. We call it frame or decoration frame and frameGeometry() returns exactly that: window size and position which includes OS decorations.On the other side geometry() returs QWidget inner rect which is available for other child controls or painting.See http://doc.qt.io/qt-4.8/application-windows.html#window-geometry for more details.Toplevel geometry() / frameGeometry() differs if our window is not frameless or fullscreen ... or we are talking about some frameless window manager under x11.

Stegall answered 11/3, 2016 at 18:55 Comment(0)
A
1

this is an old post, but that could help those searching for the same problem.

Just call

adjustSize(); 

before prompting for some geometry attributes

Artistic answered 28/3, 2017 at 14:59 Comment(0)
A
0

As user fasked notes, frameGeometry() may not include the frame margins early in the window creation lifecycle. I have found that the following code works in some situations where calling frameGeometry() does not.

QMargins frameMargins;
QWindow *window = widget->windowHandle();
if (window) {
    window->create();
    frameMargins = window->frameMargins();
}

QRect myFrameGeometry = widget->geometry().adjusted( 
        -frameMargins.left(), -frameMargins.top(),
        frameMargins.right(), frameMargins.bottom());
Avidin answered 24/3, 2021 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.