Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store the window size between sessions in Qt?

Tags:

I have a QMainWindow in a Qt application. When I close it I want it to store its current restore size (the size of the window when it is not maximized). This works well when I close the window in restore mode (that is, not maximized). But if I close the window if it is maximized, then next time i start the application and restore the application (because it starts in maximized mode), then it does not remember the size it should restore to. Is there a way to do this?

like image 319
Marius Avatar asked Sep 16 '08 17:09

Marius


2 Answers

I found that a combination of all the previous answers here was necessary on Fedora 14. Be careful not to save the size and position when the window is maximized!

void MainWindow::writePositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    qsettings.setValue( "geometry", saveGeometry() );
    qsettings.setValue( "savestate", saveState() );
    qsettings.setValue( "maximized", isMaximized() );
    if ( !isMaximized() ) {
        qsettings.setValue( "pos", pos() );
        qsettings.setValue( "size", size() );
    }

    qsettings.endGroup();
}

void MainWindow::readPositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    restoreGeometry(qsettings.value( "geometry", saveGeometry() ).toByteArray());
    restoreState(qsettings.value( "savestate", saveState() ).toByteArray());
    move(qsettings.value( "pos", pos() ).toPoint());
    resize(qsettings.value( "size", size() ).toSize());
    if ( qsettings.value( "maximized", isMaximized() ).toBool() )
        showMaximized();

    qsettings.endGroup();
}

In main(), the position settings are read before showing the window the first time...

MainWindow mainWindow;
mainWindow.readPositionSettings();
mainWindow.show();

...and these event handlers update the settings as necessary. (This causes a writes to the settings file for every mouse movement during the move and resize which is not ideal.)

void MainWindow::moveEvent( QMoveEvent* )
{
    writePositionSettings();
}

void MainWindow::resizeEvent( QResizeEvent* )
{
    writePositionSettings();
}

void MainWindow::closeEvent( QCloseEvent* )
{
    writePositionSettings();
}

Still, the vertical component of the position is not quite right, it seems to be ignoring the height of the window title bar... if anyone knows how to deal with that let me know :)

like image 123
iforce2d Avatar answered Oct 11 '22 00:10

iforce2d


Use the QWidget::saveGeometry feature to write the current settings into the registry.(The registry is accessed using QSettings). Then use restoreGeometry() upon startup to return to the previous state.

like image 43
Colin Jensen Avatar answered Oct 11 '22 02:10

Colin Jensen