A common requirement for applications is to save user settings when an application is closed and then restore them the next time the application is opened. This example shows how to save information about the application and text the user has entered.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
protected:
void closeEvent(QCloseEvent *event) override;
private:
void loadSettings();
void saveSettings();
QLineEdit *m_text;
};
This declaration for MainWindow declares a protected method which will be called when the program is closed. There are also two private slot methods and one line edit widget declared. Data entered in the text control will be saved as a setting.
MainWindow::MainWindow()
{
setMinimumSize(500, 225);
QLabel *label = new QLabel("Text stored in Settings:");
m_text = new QLineEdit();
QPushButton *pb_load = new QPushButton();
pb_load->setText("Load Settings");
QPushButton *pb_save = new QPushButton();
pb_save->setText("Save Settings");
QPushButton *pb_close = new QPushButton();
pb_close->setText("Close");
QHBoxLayout *layout_1 = new QHBoxLayout();
layout_1->addWidget(label);
layout_1->addWidget(m_text);
QHBoxLayout *layout_2 = new QHBoxLayout();
layout_2->addStretch();
layout_2->addWidget(pb_load);
layout_2->addSpacing(10);
layout_2->addWidget(pb_save);
layout_2->addSpacing(10);
layout_2->addWidget(pb_close);
layout_2->addStretch();
QVBoxLayout *layout_main = new QVBoxLayout(this);
layout_main->setContentsMargins(30, 50, 30, 15);
layout_main->addLayout(layout_1);
layout_main->addStretch();
layout_main->addLayout(layout_2);
connect(pb_load, &QPushButton::clicked,
this, &MainWindow::loadSettings);
connect(pb_save, &QPushButton::clicked,
this, &MainWindow::saveSettings);
connect(pb_close, &QPushButton::clicked,
this, &QWidget::close);
loadSettings();
}
The user interface for this code sets up a label, a line edit control, and three push buttons. The data entered for m_text will be saved as a setting when the user clicks the “Save Settings” push button and also on exit of the application. The saved value will be retrieved on line 47 when the application is opened again.
On Windows the native settings are typically saved to the Windows registry and on Unix and OS X the settings are saved to a file.
- Windows
- HKEY_CURRENT_USER\SOFTWARE\CopperSpice Journal\Example 20
- Unix
- ~/.config/CopperSpice Journal/Example 20.conf
- OS X
- ~/Library/Preferences/com.CopperSpice Journal.Example 20.plist
Signal / Slot Connections
Line 38 sets up a connection to load the previously saved settings and line 41 is a signal / slot connection to save the settings. The slots are triggered by clicking the corresponding push button.
void MainWindow::loadSettings()
{
QSettings settings("CopperSpice Journal","Example 20");
QRect appRect = settings.value("position").toRect();
if (appRect.isValid()) {
setGeometry(appRect);
}
m_text->setText(settings.value("initialText").toString());
}
void MainWindow::saveSettings()
{
QSettings settings("CopperSpice Journal","Example 20");
settings.setValue("position", this->geometry());
settings.setValue("initialText", m_text->text());
}
void MainWindow::closeEvent(QCloseEvent *event)
{
saveSettings();
}
This block of code shows the implementation of the three slot methods specified in the calls to connect(). The loadSettings() and saveSettings() both begin by constructing a new QSettings object using the overload which passes two strings. The third parameter can be used to specify the parent and in our code the default will be used.
On line 5 the previously saved value for the “window application position” is retrieved and then assigned to a QRect object. All settings are saved as a key/value pair where the key is a QString and the value is a QVariant. The QVariant class in CopperSpice is a combination of the std::any and std::variant classes.
When loading the settings the value for “position” must be converted from a QVariant to QRect by calling toRect(). The “initialText” value is converted from a QVariant to QString by calling toString().
The data type for geometry() on line 18 is a QRect and it will be converted to a QVariant in the call to setValue(). A similar conversion to a QVariant occurs for the call to text() on line 19.
The closeEvent() method on line 22 is called automatically by the event system on exit of a program or by closing a window. This is a protected method and no explicit event registration is required. This method was added to force a call to saveSettings() on program completion.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_20”.