Important: Please read the Qt Code of Conduct -

Main Window Repositioning

  • We save the position of the window when our app was closed and then restore it when you run it again. However, if the screen resolution has changed, it can end up off the screen. I've tried the following code and it almost works:

    @ // restore size and location
    QSize size(settings->value("size", QSize(340, 600)).toSize());
    QPoint pos(settings->value("pos", QPoint(200, 200)).toPoint());

    QDesktopWidget* desktop = QApplication::desktop();
    int screenwidth(desktop->frameGeometry().width());
    int screenheight(desktop->frameGeometry().height());
    int windowwidth(QMainWindow::frameGeometry().width());
    int windowheight(QMainWindow::frameGeometry().height());
    if ( (pos.x()+windowwidth) > screenwidth)
        pos.setX(screenwidth - windowwidth);
    if ( (pos.y()+windowheight) > screenheight)
        pos.setY(screenheight - windowheight);


    The problem is that the window is still offscreen a little bit. Nothing major but it sure looks like the frame still isn't getting accounted for. If I stop it in the debugger, I see that the 'size' height and width are the same as windowheight and windowwidth, which doesn't sound possible. If I'm reading the docs right, the size I resize to can't be the same as the frameGeometry size, as the former doesn't include decorations but the latter does.

  • you repositiopn it, but you don't resize it, that is the problem.
    x should never be smaller then 0 and with should not be bigger than screenwidth.
    same applies to y and heigth.

    if you adjust by these, it should work:

    bool adjustToScreen(QRect& rRect)
    QDesktopWidget* desktop = QApplication::desktop();
    QRect rcDesktop = desktop->frameGeometry();

    // if the original rect is not completly inside the desktop rectangle, it must be adjusted
        rRect.setLeft(qMax(rcDesktop.left(), rRect.left()));
        rRect.setRight(qMin(rcDesktop.right(), rRect.right()));
        rRect.setBottom(qMin(rcDesktop.bottom(), rRect.bottom()));
        return true;
    return false;


    void MainWindow::restore()
    QSettings settings;
    // restore size and location
    QSize size(settings.value("size", QSize(340, 600)).toSize());
    QPoint pos(settings.value("pos", QPoint(200, 200)).toPoint());

    QRect rc = frameGeometry();
        QSize ncSize = frameGeometry().size() - geometry().size();
        resize(rc.size() - ncSize);



  • But I don't want to resize it again. I should just be able to move the window more precisely onto the desktop.

    It is nice looking code, but I don't think the rRect repositionings work. I just want to move the window back onto the screen.

  • You could use libqxt

    LibQxt is an extension library for Qt providing a suite of cross-platform utility classes to add functionality not readily available in the Qt toolkit by Qt Development Frameworks, Nokia.
    The QxtScreen class provides access to screen settings.
    Note: Currently supported platforms are X11 and Windows.

    QSize QxtScreen::resolution () const
    Returns the current resolution.

    You could get the screen resolution and set the size/position of your window accordingly.

    You may have no choice but to resize depending on the end users screen resolution. My current monitor is a 15inch flat screen. I get a max resolution of 1024, 768 and 800, 600 window takes up most of the desktop.

  • Hi jdarnold,

    if you don't want to resize, you could just use the move command, and let the resize out.

    checks, that the given rectangle is inside the current screen.

Log in to reply