Sunday, December 16, 2012

GUI – Settings and Window Title

All programs should save all of their settings upon exit and restore them so that when restarted the program starts up in exactly the way it was when it exited, because otherwise it is time consuming to set all the settings again.  Very few programs do however, but this approach will be used for this project.

Fortunately, Qt has the QSettings class to perform the saving and restoring mostly automatically.  The vendor (programmer) name and program name is provided to the constructor of the QSettings instance.  For saving, the setValue() method function is used with the name of the property to save and the value to save.  When the instance goes out of scope, the settings are saved.  For restoring, the settings are automatically read when the instance is created.  The properties are accessed with the value() method function.

The settings are stored to a platform specific location.  On Windows, this is to the registry.  On Linux, this is to a text file located in the .config directory on the user's home directory within a sub-directory for the vendor name under another sub-directory for the program name.  The file name is the program name with the .conf extension.

The settings are restored in the MainWindow constructor.  For saving, the closeEvent() function was implemented.  This function is called when a close event occurs (for example, the File/Exit menu item, the close icon, etc.) and can decide whether to accept or reject the request.  For now, the settings are saved and the event is accepted.

For now, only the position and the size of the main window is saved and restored.  These are obtained from the geometry of the MainWindow instance.  The program will now remember where it was and how big the last time the program was closed.  Additional settings will be added as the are implemented in the program.

[commit 54a5b24f69]