Results 1 to 2 of 2

Thread: Restore the mainwindow size on last open

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    30
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Restore the mainwindow size on last open

    Hello Everyone,

    I just want to restore the size of window of last open window. suppose i have open a window and its size is (500,500) and i have resize it to (600,600).
    I have closed the window and again open the project this time window size should be (600,600).
    Any help will be appreciable

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Restore the mainwindow size on last open

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Restore the size of the window after minimize
    By SkripT in forum Qt Programming
    Replies: 10
    Last Post: 22nd September 2016, 17:23
  2. Replies: 6
    Last Post: 26th December 2015, 18:33
  3. Replies: 0
    Last Post: 9th December 2014, 18:30
  4. Replies: 4
    Last Post: 20th November 2009, 13:25
  5. Open mainwindow
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 17th April 2009, 23:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.