|  | Home · Overviews · Examples |

# QMainWindow Class Reference
[com.trolltech.qt.gui module]

The `QMainWindow` class provides a main application window. More...

Inherits `QWidget`.

## Detailed Description

The `QMainWindow` class provides a main application window.

- Qt Main Window Framework
- Creating Main Window Components

- Creating Menus
- Creating Toolbars
- Creating Dock Widgets
- The Status Bar

- Storing State

### Qt Main Window Framework

A main window provides a framework for building an application's user interface. Qt has `QMainWindow` and its related classes for main window management. `QMainWindow` has its own layout to which you can add `QToolBar`s, `QDockWidget`s, a `QMenuBar`, and a `QStatusBar`. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.

### Creating Main Window Components

A central widget will typically be a standard Qt widget such as a `QTextEdit` or a `QGraphicsView`. Custom widgets can also be used for advanced applications. You set the central widget with `setCentralWidget()`.

Main windows have either a single (SDI) or multiple (MDI) document interface. You create MDI applications in Qt by using a `QMdiArea` as the central widget.

We will now examine each of the other widgets that can be added to a main window. We give examples on how to create and add them.

#### Creating Menus

Qt implements menus in `QMenu` and `QMainWindow` keeps them in a `QMenuBar`. QActions are added to the menus, which display them as menu items.

You can add new menus to the main window's menu bar by calling `menuBar()`, which returns the `QMenuBar` for the window, and then add a menu with QMenuBar::addMenu().

`QMainWindow` comes with a default menu bar, but you can also set one yourself with `setMenuBar()`. If you wish to implement a custom menu bar (i.e., not use the `QMenuBar` widget), you can set it with `setMenuWidget()`.

An example of how to create menus follows:

        void MainWindow::createMenus()
        {
            fileMenu = menuBar()->addMenu(tr("&File"));
            fileMenu->addAction(newAct);
            fileMenu->addAction(openAct);
            fileMenu->addAction(saveAct);

The `createPopupMenu()` function creates popup menus when the main window receives context menu events. The default implementation generates a menu with the checkable actions from the dock widgets and toolbars. You can reimplement `createPopupMenu()` for a custom menu.

#### Creating Toolbars

Toolbars are implemented in the `QToolBar` class. You add a toolbar to a main window with `addToolBar()`.

You control the initial position of toolbars by assigning them to a specific Qt::ToolBarArea. You can split an area by inserting a toolbar break - think of this as a line break in text editing - with `addToolBarBreak()` or `insertToolBarBreak()`. You can also restrict placement by the user with QToolBar::setAllowedAreas() and QToolBar::setMovable().

The size of toolbar icons can be retrieved with `iconSize()`. The sizes are platform dependent; you can set a fixed size with `setIconSize()`. You can alter the appearance of all tool buttons in the toolbars with `setToolButtonStyle()`.

An example of toolbar creation follows:

        void MainWindow::createToolBars()
        {
            fileToolBar = addToolBar(tr("File"));
            fileToolBar->addAction(newAct);

#### Creating Dock Widgets

Dock widgets are implemented in the `QDockWidget` class. A dock widget is a window that can be docked into the main window. You add dock widgets to a main window with `addDockWidget()`.

There are four dock widget areas as given by the Qt::DockWidgetArea enum: left, right, top, and bottom. You can specify which dock widget area that should occupy the corners where the areas overlap with `setDockWidgetCorner()`. By default each area can only contain one row (vertical or horizontal) of dock widgets, but if you enable nesting with `setDockNestingEnabled()`, dock widgets can be added in either direction.

Two dock widgets may also be stacked on top of each other. A `QTabBar` is then used to select which of the widgets that should be displayed.

We give an example of how to create and add dock widgets to a main window:

            QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this);
            dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
                                        Qt::RightDockWidgetArea);
            dockWidget->setWidget(dockWidgetContents);
            addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

#### The Status Bar

You can set a status bar with `setStatusBar()`, but one is created the first time `statusBar()` (which returns the main window's status bar) is called. See `QStatusBar` for information on how to use it.

### Storing State

`QMainWindow` can store the state of its layout with `saveState()`; it can later be retrieved with `restoreState()`. It is the position and size (relative to the size of the main window) of the toolbars and dock widgets that are stored.

See also `QMenuBar`, `QToolBar`, `QStatusBar`, `QDockWidget`, Application Example, Dock Widgets Example, MDI Example, SDI Example, and Menus Example.