Important: Please read the Qt Code of Conduct -

Force resize of window when geometry is the same?

  • I have a frameless window that I'm maximizing then restoring, but I'm having an issue with the window size not being restored properly if it's greater than the monitor it was maximized in in a dual monitor setup.

    My solution was to store the geometry before it was maximized then restore it when the window itself is restored. The problem is that the window ignores a resize if the geometry is the same, so my solution is to make the width one pixel wider then immediately set the geometry back:

    QWidget::window()->resize(m_normal_rect.width() + 1,

    Is there a way to force the geometry to update even if it's the same as the current geometry?

  • Lifetime Qt Champion


    Do you mean that your user can resize that frameless window ?

  • Yes, it's a frameless window being used to create a window with a custom style.

  • Lifetime Qt Champion

    How do they resize it ?

  • Like a normal window, using the mouse cursor and dragging it.

    Edit: Which is functionality that I added. When the user clicks+drags, I call window()->setGeometry(...) with the updated geometry.

  • Lifetime Qt Champion

    Then the silly question: why do you to resize if your widget already has the correct size on maximisation ?

  • Because if the normal geometry's width is greater than the width of the monitor that it was maximized on, when it's restored the window is truncated to the width of the monitor. The first resize forces setGeometry_sys(...) to actually resize the window, then the second resize returns it to it's correct size.

    The real issue is that the restored window isn't restoring to it's correct size, but I haven't found a way to fix that so the resizing is just a workaround.

  • Lifetime Qt Champion

    Then why not use QDesktopWidget::screenGeometry and use it to ensure your resize is done using the best value possible ?

  • What do you mean best value?

    I use the screen geometry to set the maximized size (using win32 functions).

  • Lifetime Qt Champion

    How are you doing that ?

  • In a nativeEventFilter I check for a WM_GETMINMAXINFO message, and do this:

        auto maximize_dimensions = reinterpret_cast<::MINMAXINFO*>(msg->lParam);
        auto rect = QApplication::desktop()->availableGeometry(this);
        maximize_dimensions->ptMaxSize.x = rect.width();
        maximize_dimensions->ptMaxSize.y = rect.height() - 1;
        maximize_dimensions->ptMaxPosition.x = 0;
        maximize_dimensions->ptMaxPosition.y = 0;

    Which is used to properly maximize the window in all monitors on a multiple monitor setup.

    Then on state changes:

        if(event->type() == QEvent::WindowStateChange) {
          auto state_change_event = static_cast<QWindowStateChangeEvent*>(event);
              && QWidget::window()->isMaximized()) {
            m_normal_rect = QWidget::window()->frameGeometry();
          } else if(state_change_event->oldState().testFlag(Qt::WindowMaximized) &&
              !QWidget::window()->isMaximized()) {
            QWidget::window()->resize(m_normal_rect.width() + 1,

    Which fixes the issue I mentioned in posts 1 and 7 when restoring the window. I couldn't find a way to fix it using the Windows' API, so my double resize method is how I've fixed it.

    I should have mentioned that when I get the window geometry after the window is restored, it gives me the correct size, but Windows isn't showing the window at that size. There's something in resizing the window that fixes the size discrepancy between the system window and Qt's window.

  • So is there no way to force a resize of a window?

  • Lifetime Qt Champion

    Well, forcing a resize means that you have to change it at some point.

Log in to reply