Important: Please read the Qt Code of Conduct -

Widget geometry issue

  • Hi! I want to save window geometry to the registry. It all works fine but sometimes I get QWindowsWindow::setGeometry: Unable to set geometry error.

    And registry data is @Rect(32775 32798 1334 600)
    x - 32775
    y - 32798

    I think it's out of bounds of my screen. How to get max X and Y of the screen to check values before setting the geometry. Thanks.

  • Moderators

    Usually this is caused by having the app on a secondary screen. I usually check desktops and make sure if I'm on a system that doesn't have multiple screens I restore my geometry to the main window.

    Something like:

        auto desktop = QApplication::desktop();
        auto screenLocation = desktop->screenNumber(this);
        if (screenLocation == -1)
            qWarning() << "Main window is not on a valid screen, moving to main screen";
            move(20, 20);

    If that isn't what the issue is, then to answer your question you can get the screen geometry with:

    const QRect QDesktopWidget::screenGeometry(int screen = -1) const

    And you can get the desktop widget via application like I did above.

    I would figure out why you are having bad data saved for the geometry though.

  • @ambershark

    I have fixed it by setting nativeEvent and checking for message - SIZE_MAXIMIZED. When window is maximized the values are not saved. Thanks.

Log in to reply