Use QSettings to save / retrieve the size and position of your window. Override the QWidget::showEvent() and QWidget::closeEvent() methods for your window class and save and restore the setting there:

Qt Code:
  1. // Helper class
  2.  
  3. // QtSettingsHelper.h
  4.  
  5. #include <QVariant>
  6.  
  7. class QString;
  8.  
  9. class CQtSettingsHelper
  10. {
  11. private:
  12. virtual ~CQtSettingsHelper() = 0;
  13.  
  14. public:
  15. static QVariant ReadValue( const QString & group, const QString & key, const QVariant & defValue = QVariant() );
  16. static void WriteValue( const QString & group, const QString & key, const QVariant & value );
  17. };
  18.  
  19. // QtSettingsHelper.cpp
  20.  
  21. #include "SAQtSettingsHelper.h"
  22.  
  23. #include <QCoreApplication>
  24. #include <QSettings>
  25.  
  26. QVariant CQtSettingsHelper::ReadValue( const QString & group, const QString & key, const QVariant & defValue )
  27. {
  28. QSettings settings( QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName() );
  29.  
  30. settings.beginGroup( group );
  31. QVariant value = settings.value( key, defValue );
  32. settings.endGroup();
  33.  
  34. return value;
  35. }
  36.  
  37. void CQtSettingsHelper::WriteValue( const QString & group, const QString & key, const QVariant & value )
  38. {
  39. QSettings settings( QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName() );
  40.  
  41. settings.beginGroup( group );
  42. settings.setValue( key, value );
  43. settings.endGroup();
  44. }
  45.  
  46. // Example how to use to save / restore window sizes
  47.  
  48. // Mainindow.h:
  49.  
  50. class CMainWindow
  51. : public QMainWindow
  52. {
  53. Q_OBJECT
  54.  
  55. public:
  56. CMainWindow( QWidget * pParent = 0 );
  57. ~CMainWindow();
  58.  
  59. protected:
  60. virtual void closeEvent( QCloseEvent * pEvent );
  61. virtual void showEvent( QShowEvent * pEvent );
  62.  
  63. private:
  64. void restoreSettings();
  65. void saveSettings();
  66. };
  67.  
  68. // MainWindow.cpp
  69.  
  70. void CMainWindow::closeEvent( QCloseEvent * pEvent )
  71. {
  72. saveSettings();
  73. pEvent->accept();
  74. }
  75.  
  76. void CMainWindow::showEvent( QShowEvent * pEvent )
  77. {
  78. restoreSettings();
  79. pEvent->accept();
  80.  
  81. }
  82.  
  83. static const int sStateVersion = 1000; // Used for versioning state information
  84.  
  85.  
  86. void CMainWindow::restoreSettings()
  87. {
  88. QString path = QStandardPaths::writableLocation( QStandardPaths::DocumentsLocation );
  89.  
  90. resize( CQtSettingsHelper::ReadValue( "MainWindow", "Size", QSize( 800, 800 ) ).toSize() );
  91. move( CQtSettingsHelper::ReadValue( "MainWindow", "Position", QPoint( 50, 50 ) ).toPoint() );
  92. restoreState( CQtSettingsHelper::ReadValue( "MainWindow", "State" ).toByteArray(), sStateVersion );
  93.  
  94. }
  95.  
  96. void CMainWindow::saveSettings()
  97. {
  98. CQtSettingsHelper::WriteValue( "MainWindow", "Size", size() );
  99. CQtSettingsHelper::WriteValue( "MainWindow", "Position", pos() );
  100. CQtSettingsHelper::WriteValue( "MainWindow", "State", saveState( sStateVersion ) );
  101. }
To copy to clipboard, switch view to plain text mode 

In this code, I am saving not only the size and position of my main window, but also the state, which contains information on the layout of menu, tab, and status bars as well as any dock widgets. Since saveState() and restoreState() and QMainWindow methods, they will not work for other QWidget classes.

The helper class makes it easier to pass arguments to QSettings for save or restore.