Storing Settings

In the MainWindow constructor, we called readSettings() to load the application's stored settings. Similarly, in closeEvent(), we called writeSettings() to save the settings. These two functions are the last MainWindow member functions that need to be implemented.

The arrangement we opted for in MainWindow, with all the QSettings-related code in readSettings() and writeSettings(), is just one of many possible approaches. A QSettings object can be created to query or modify some setting at any time during the execution of the application and from anywhere in the code.

void MainWindow::writeSettings()
 QSettings settings;
 settings.setPath("", "Spreadsheet");
 settings.writeEntry("/geometry/x", x());
 settings.writeEntry("/geometry/y", y());
 settings.writeEntry("/geometry/width", width());
 settings.writeEntry("/geometry/height", height());
 settings.writeEntry("/recentFiles", recentFiles);
 settings.writeEntry("/showGrid", showGridAct->isOn());
 settings.writeEntry("/autoRecalc", showGridAct->isOn());

The writeSettings() function saves the main window's geometry (position and size), the list of recently opened files, and the Show Grid and Auto-recalculate options.

QSettings stores the application's settings in platform-specific locations. On Windows, it uses the system registry; on Unix, it stores the data in text files; on Mac OS X, it uses the Carbon preferences API. The setPath() call provides QSettings with the organization's name (as an Internet domain name) and the product's name. This information is used in a platform-specific way to find a location for the settings.

QSettings stores settings as keyvalue pairs. The key is similar to a file system path and should always start with the name of the application. For example, /Spreadsheet/geometry/x and /Spreadsheet/showGrid are valid keys. (The beginGroup() call saves us from writing /Spreadsheet in front of every key.) The value can be an int, a bool, a double, a QString, or a QStringList.

void MainWindow::readSettings()
 QSettings settings;
 settings.setPath("", "Spreadsheet");

 int x = settings.readNumEntry("/geometry/x", 200);
 int y = settings.readNumEntry("/geometry/y", 200);
 int w = settings.readNumEntry("/geometry/width", 400);
 int h = settings.readNumEntry("/geometry/height", 400);
 resize(w, h);

 recentFiles = settings.readListEntry("/recentFiles");

 settings.readBoolEntry("/showGrid", true));
 settings.readBoolEntry("/autoRecalc" true));


The readSettings() function loads the settings that were saved by writeSettings(). The second argument to the "read" functions specifies a default value, in case there are no settings available. The default values are used the first time the application is run.

We have now completed the Spreadsheet's MainWindow implementation. In the following sections, we will discuss how the Spreadsheet application can be modified to handle multiple documents and how to implement a splash screen. We will complete its functionality in the next chapter.

Part I: Basic Qt

Getting Started

Creating Dialogs

Creating Main Windows

Implementing Application Functionality

Creating Custom Widgets

Part II: Intermediate Qt

Layout Management

Event Processing

2D and 3D Graphics

Drag and Drop


Container Classes





Providing Online Help


Platform-Specific Features

C++ GUI Programming with Qt 3
C++ GUI Programming with Qt 3
ISBN: 0131240722
EAN: 2147483647
Year: 2006
Pages: 140 © 2008-2020.
If you may any questions please contact us: