QMainWindow and QSettings

Table of contents:

Most QApplications manage a single QMainWindow. As Figure 11.4 shows, the QMainWindow has some features that are common to most desktop applications:

  • A central widget
  • Menu bar
  • Status bar
  • Dock regions

Figure 11.4. A main window

Because the QMainWindow is the parent of all other widgets (in that main window), it is common practice to extend that class for some applications, as shown in Example 11.1.

Example 11.1. src/widgets/mainwindow/mymainwindow.h

[ . . . . ]
class MyMainWindow : public QMainWindow {
 Q_OBJECT

public:
 MyMainWindow();
 void closeEvent(QCloseEvent *event); <-- 1

protected slots:
 virtual void newFile();
 virtual void open();
 virtual bool save();
[ . . . . ]
 

(1)overridden from base class to capture when the user wants to close the window

11.2.1. QSettings: Saving and Restoring Application State

All modern desktop applications have a way for users to configure the settings. The settings/preferences/options need to be persistent. The mechanism for that is included with QSettings. As you develop a new QMainWindow application, the first persistent settings you may want to save will probably be the window size and position. You may also want to save the names of the most recent documents that were opened by the application.

QSettings is a persistent map of key/value pairs. It is a QObject and uses QObject's property interface, setValue() and value(), to set and get its values. It can be used to store any data that needs to be remembered across multiple executions.

The QSettings constructor has two QString parameters: one for the organization name and one for the application name. You can establish defaults for these values with the two QCoreApplication functions, setOrganizationName() and setApplicationName(), after which you can use the default QSettings constructor. Each combination of names defines a unique persistent map that does not clash with settings from other-named Qt applications.

Monostate Pattern

A class that allows multiple instances to share the same state is an implementation of the Monostate pattern. Two instances of QSettings with the same organization/application name can be used to access the same persistent map data. This makes it easy for applications to access common settings from different source files.

QSettings is an implementation of the Monostate pattern.

The actual mechanism for the persistent storage of QSettings data is implementation dependent and quite flexible. Some possibilities for its storage include the Win32 registry (in Windows) and your $HOME/.qt directory (in Linux). For more detailed information, see the Qt QSettings API documentation.

QMainWindow::saveState() returns a QByteArray that contains information about the main window's toolbars and dockwidgets. To do this it makes use of the objectName property for each of those subwidgets, thus making it important that each name be unique. saveState() has an optional int versionNumber parameter. The QSettings object stores that QByteArray with the key string "state".

QMainWindow::restoreState() takes a QByteArray, presumably created by saveState(), and uses the information that it holds to put the toolbars and dockwidgets into a prior arrangement. It too has an optional versionNumber parameter. We show the use of these functions in Example 11.2.

Example 11.2. src/widgets/mainwindow/mymainwindow.cpp

[ . . . . ]

void MyMainWindow::readSettings() {
 QSettings settings("objectlearning.net", "Qt4 Sample Main"); <-- 1
 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
 QSize size = settings.value("size", QSize(400, 400)).toSize();
 QByteArray state = settings.value("state", QByteArray()).toByteArray();
 restoreState(state);
 resize(size);
 move(pos);
}

void MyMainWindow::writeSettings() {
 /* Save postion/size of main window */

 QSettings settings("objectlearning.net", "Qt4 Sample Main");
 settings.setValue("pos", pos());
 settings.setValue("size", size());
 settings.setValue("state", saveState());
}
 

(1)The constructor takes the organization name and the app name as arguments.

By placing the "root program logic" in a derived class of QMainWindow or QApplication, the main() becomes quite simple, as shown in Example 11.3.

Example 11.3. src/widgets/mainwindow/mainwindow-main.cpp

#include "mymainwindow.h"
#include 

int main( int argc, char ** argv ) {
 QApplication a( argc, argv );
 MyMainWindow mw;
 mw.show();
 return a.exec();
}


Dialogs

Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net