<p>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?</p>

[{'answer_id': 74885, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 2, 'selected': False, 'text': "<p>I've encountered this problem as well.</p>\n\n<p>What you can do: in addition to the window's size, save whether it's maximized or not (<code>QWidget::isMaximized()</code>). </p>\n\n<p>Then next time you start the application, first set the size (<code>QWidget::resize()</code>) and then maximize it if appropriate (<code>QWidget::showMaximized()</code>). When it's restored, it should return to the correct size.</p>\n"}, {'answer_id': 76699, 'author': 'Colin Jensen', 'author_id': 9884, 'author_profile': 'https://Stackoverflow.com/users/9884', 'pm_score': 5, 'selected': True, 'text': '<p>Use the <a href="http://doc.qt.io/qt-4.8/qwidget.html#saveGeometry" rel="nofollow noreferrer">QWidget::saveGeometry</a> 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.</p>\n'}, {'answer_id': 8736705, 'author': 'iforce2d', 'author_id': 624593, 'author_profile': 'https://Stackoverflow.com/users/624593', 'pm_score': 4, 'selected': False, 'text': '<p>I found that a combination of all the previous answers here was necessary on Fedora 14. Be careful <strong>not</strong> to save the size and position when the window is maximized!</p>\n\n<pre><code>void MainWindow::writePositionSettings()\n{\n    QSettings qsettings( "iforce2d", "killerapp" );\n\n    qsettings.beginGroup( "mainwindow" );\n\n    qsettings.setValue( "geometry", saveGeometry() );\n    qsettings.setValue( "savestate", saveState() );\n    qsettings.setValue( "maximized", isMaximized() );\n    if ( !isMaximized() ) {\n        qsettings.setValue( "pos", pos() );\n        qsettings.setValue( "size", size() );\n    }\n\n    qsettings.endGroup();\n}\n\nvoid MainWindow::readPositionSettings()\n{\n    QSettings qsettings( "iforce2d", "killerapp" );\n\n    qsettings.beginGroup( "mainwindow" );\n\n    restoreGeometry(qsettings.value( "geometry", saveGeometry() ).toByteArray());\n    restoreState(qsettings.value( "savestate", saveState() ).toByteArray());\n    move(qsettings.value( "pos", pos() ).toPoint());\n    resize(qsettings.value( "size", size() ).toSize());\n    if ( qsettings.value( "maximized", isMaximized() ).toBool() )\n        showMaximized();\n\n    qsettings.endGroup();\n}\n</code></pre>\n\n<p>In main(), the position settings are read before showing the window the first time...</p>\n\n<pre><code>MainWindow mainWindow;\nmainWindow.readPositionSettings();\nmainWindow.show();\n</code></pre>\n\n<p>...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.)</p>\n\n<pre><code>void MainWindow::moveEvent( QMoveEvent* )\n{\n    writePositionSettings();\n}\n\nvoid MainWindow::resizeEvent( QResizeEvent* )\n{\n    writePositionSettings();\n}\n\nvoid MainWindow::closeEvent( QCloseEvent* )\n{\n    writePositionSettings();\n}\n</code></pre>\n\n<p>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 :)</p>\n'}, {'answer_id': 10866437, 'author': 'Raimar', 'author_id': 894142, 'author_profile': 'https://Stackoverflow.com/users/894142', 'pm_score': 0, 'selected': False, 'text': '<p>The image at <a href="http://qt-project.org/doc/qt-4.8/application-windows.html" rel="nofollow">http://qt-project.org/doc/qt-4.8/application-windows.html</a> shows, that <code>geometry.x()</code> and <code>geometry.y()</code> are not equal to <code>x()</code> and <code>y()</code>, which are the same as <code>pos()</code>.</p>\n\n<p>In my case, I use:</p>\n\n<pre><code>x()\ny()\nwidth()\nheight()\n</code></pre>\n\n<p>and restore these successfully with: </p>\n\n<pre><code>move()\nresize()\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/74690', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1585/']