1

the difference of the upper left corner between frameGeometry() and geometry() is x= 8 and y = 31 pixel. While the last seems to be correct, the value of 8 is much too high (should be one). I have written the code snippet below to demonstrate it. The program is overlaying 2 windows, the second being shifted by the difference in position of frameGeometry() and geometry(). So the upper left corner of the second window should be located at the upper left corner of the client area of the first window, what obviously is not the case, see picture attached.

// main_test_frameGeometry.cpp

#include <QApplication>
#include <QtWidgets>

// values for Windows 10
const int frameThickness = 1
const int titlebarThickness = 30;

QRect widgetRectangle( const QWidget& widget)         // Ersatz for frameGeometry(). Gives right values *before* and after widget has been drawn
{
     return QRect(widget.frameGeometry().x(),  widget.frameGeometry().y(), widget.width()+2*frameThickness, widget.height()+titlebarThickness+2*frameThickness);
}
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget* widget1 = new QWidget();
    widget1->setWindowTitle("widget 1");
    widget1->resize(300, 200);
    qDebug() << "widget1                                   geometry:" << widget1->geometry() << "    | frameGeometry :" << widget1->frameGeometry() << "    | widgetRectangle:" << widgetRectangle( *widget1 );
    widget1->move(100, 100);
    qDebug() << "widget1 - after move(100,100)             geometry:" << widget1->geometry() << "| frameGeometry :" << widget1->frameGeometry() << "| widgetRectangle:" << widgetRectangle( *widget1 );
    widget1->show();
    qDebug() << "widget1 - after move(100,100) & show()    geometry:" << widget1->geometry() << "| frameGeometry :" << widget1->frameGeometry() << "| widgetRectangle:" << widgetRectangle( *widget1 );
    QWidget* widget2 = new QWidget();
    widget2->setWindowTitle("widget 2");
    widget2->resize(300, 200);
    widget2->adjustSize();
    widget2->move(108, 131);
    widget2->show();

    qDebug() << "widget2 - after move(108,131) & show()    geometry:" << widget1->geometry() << "| frameGeometry :" << widget1->frameGeometry() << "| widgetRectangle:" << widgetRectangle( *widget2 );
    qDebug() << "\nunder Windows 10 Qt is assuming that frameThickness is 8 pixel and so give wrong values \nfor geometry().x(), framegeometry().width()and frameGeometry().height()!";
    qDebug() << "\nframeGeometry() gives before and after show() the right position values x() and y(), geometry() \nthe right size, so we build our own function widgetRectangle() taking this into account!";

    return app.exec();
}

The corresponding printout is:

widget1                                   geometry: QRect(0,0 300x200)     | frameGeometry : QRect(0,0 300x200)     | widgetRectangle: QRect(0,0 302x232)
widget1 - after move(100,100)             geometry: QRect(100,100 300x200) | frameGeometry : QRect(100,100 300x200) | widgetRectangle: QRect(100,100 302x232)
widget1 - after move(100,100) & show()    geometry: QRect(108,131 300x200) | frameGeometry : QRect(100,100 316x239) | widgetRectangle: QRect(100,100 302x232)
widget2 - after move(108,131) & show()    geometry: QRect(108,131 300x200) | frameGeometry : QRect(100,100 316x239) | widgetRectangle: QRect(108,131 302x232)

under Windows 10 Qt is assuming that frameThickness is 8 pixel wide and so give wrong values for geometry().x(), framegeometry().width()and frameGeometry().height()!

frameGeometry() gives before and after show() the right position values x() and y(), geometry() the right size, so we build our own function widgetRectangle(const QWidget*) taking this into account - see source code above -

This function has the advantage to give the right values before the widget is drawn, which is of advantage if you need the values to place your widget on a given position.

The values for Windows 10 seems to be: frameThickness = 1 and titlebarThickness = 30, but those vary from OS to OS.

Is there a function to query frameThickness and titlebarThickness?

enter image description here

in response to Kuba Ober, following some more explanations and precisions.

What the client area is, is defined here: http://doc.qt.io/qt-5/application-windows.html#window-geometry

I have set the background color of widget1 to yellow. In case the offset of the client area with regards to the frame postion would be correct, the client area of widget1 would be completely overlapped by the second widget2 and no yellow color should be visible. This is not the case.

To demonstrate that frameGeometry is incorrect, I have placed a third widget3 at the bottom-right corner of the screen. The coordinates of the widget has been set to:

x = screenwidth - frameGeometry().width(); // in my case x = 1919 - 316 = 1603
y = screenheight - frameGeometry().height(); // y = 1079 - 239 = 840

In case the frameGeometry would be correct, the widget would be placed at the bottom-right corner of the screen and no room would be left over. But you can clearly see some area between the widget and the lower-right corner of the screen!

The source code for widget3 is:

QWidget* widget3 = new QWidget();
widget3->setWindowTitle("widget 3");
widget3->resize(300, 200);
widget3->adjustSize();
widget3->move(1603, 840);   // 1603 = 1919 - 316, 840 = 1079 - 239
widget3->show();
qDebug() << "\nwidget3 - after move(1603,840) & show()   geometry:" << widget3->geometry() << "| frameGeometry :" << widget3->frameGeometry() << "| widgetRectangle:" << widgetRectangle( *widget3 );
//widget3 - after move(1603,840) & show()   geometry: QRect(1611,871 300x200) | frameGeometry : QRect(1603,840 316x239) | widgetRectangle: QRect(1603,840 300x232)

enter image description here

enter image description here enter image description here

Alain Weiler
  • 139
  • 1
  • 11
  • "should be located at the upper left corner of the client area of the first window, what obviously is not the case" You can't assume the "obvious" part without demonstrating that the client area is indeed what you think it is, or, alternatively, that the frame geometry is incorrect. – Kuba hasn't forgotten Monica Mar 31 '17 at 12:55

0 Answers0