'Why am I getting QWindowsWindow::setGeometry: Unable to set geometry warning with Qt 5.12.0

I migrated some code from Qt 5.6.0 to 5.12.0. Suprisingly, I'm getting lots of warnings related to QWindowsWindow::setGeometry. Whenever a dialog is shown on top of another, I get this warning.

I could isolate the problem in a MCVE, it's very simple and minimal, all parenting look good, however, we get the warning when button is pressed:

QWindowsWindow::setGeometry: Unable to set geometry 132x30+682+303 on QWidgetWindow/'QDialogClassWindow'. Resulting geometry:  132x42+682+303 (frame: 4, 28, 4, 4, custom margin: 0, 0, 0, 0, minimum size: 116x42, maximum size: 16777215x16777215).

main.cpp:

#include <QApplication>
#include "mainframe.h"
#include <qDebug>

void MessageOutput( QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    qDebug() << msg;
}

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

    qInstallMessageHandler(MessageOutput);

    MainFrame wnd;
    wnd.show();

    return app.exec();
}

mainframe.h:

#include <QMainWindow>

class QPushButton;
class MainFrame : public QMainWindow
{
    Q_OBJECT

public:
    MainFrame();

public slots:
    void showPopup();

private:
    QPushButton* button;
};

mainframe.cpp:

#include "mainframe.h"

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

MainFrame::MainFrame()
{
    QWidget* widget = new QWidget( this );
    widget->setLayout( new QVBoxLayout( widget ) );

    QPushButton* pTextButton = new QPushButton( "Show popup", widget );
    widget->layout()->addWidget( pTextButton );
    connect( pTextButton, SIGNAL(clicked()), this, SLOT(showPopup()) );

    setCentralWidget( widget );
}

void MainFrame::showPopup()
{
    QDialog dlg( this );
    dlg.setLayout( new QVBoxLayout() );
    dlg.layout()->addWidget( new QLabel("popup message",&dlg) );
    dlg.exec();
}

I see the issue under Windows 7 and 10. Am I doing anything wrong?

I know the warning can be removed by setting setMinimumSize (see https://stackoverflow.com/a/31231069/3336423), but why should we do this for every widget we create? Is there a way to fix that for good?



Solution 1:[1]

The issue was reported to Qt: https://bugreports.qt.io/browse/QTBUG-73258

To the code in OP is OK, it's just a Qt bug.

It's marked as "P2 Important", so hopefully it should be fixed in a next release.


Update: It's still not fixed in Qt 6.2.2...

Solution 2:[2]

As you mentioned, this problem occurs only in Windows: the QWindowsWindow class is part of the windows platform plugin. Looking at Qt's source code (qwindowswindow.cpp@QWindowsWindow::setGeometry) there is no direct way to pause that specific message.

The only global solution I can think of right now is to filter the warning messages using a message handler:

void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
  if (type != QtWarningMsg || !msg.startsWith("QWindowsWindow::setGeometry")) {
    QByteArray localMsg = msg.toLocal8Bit();
    fprintf(stdout, localMsg.constData());
  }
}

int main(int argc, char* argv[])
{
  qInstallMessageHandler(myMessageOutput);
  QApplication a(argc, argv);
  // ...
}

UPDATE

One of the problems is that Windows adds its own buttons to the frame. In your example the dialog adds three buttons: the system button (the icon, top-left corner), the help button and the close button. The help and close buttons have a fixed size, which happens to be larger than the QDialog's frame (which is computed as the maximum between the requested size and minimumSize). This then generates the warning: your requested size doesn't match the one created by Windows:

Dialog with help & close buttons

If you remove the help button, for example (dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowContextHelpButtonHint);), the warning disappears without setting a minimum size for the window. A manual action must be taken for each dialog displayed, but I think it is easier to automatize than the minimum size (through a factory maybe?):

Dialog without help button

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2