- Qt 6.10
- Qt Widgets
- C++ Classes
- QMainWindow
- Qt 6.10.2 Reference Documentation

# QMainWindow Class

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

Header: | `#include <QMainWindow>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Widgets)` `target_link_libraries(mytarget PRIVATE Qt6::Widgets)`
qmake: | `QT += widgets`
Inherits: | QWidget

- List of all members, including inherited members

## Public Types

enum | DockOption { AnimatedDocks, AllowNestedDocks, AllowTabbedDocks, ForceTabbedDocks, VerticalTabs, GroupedDragging }
flags | DockOptions

## Properties

- dockOptions : DockOptions
- iconSize : QSize
- toolButtonStyle : Qt::ToolButtonStyle

## Public Functions

| QMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
virtual | ~QMainWindow()
QWidget * | centralWidget() const
QMainWindow::DockOptions | dockOptions() const
QSize | iconSize() const
bool | restoreState(const QByteArray &state, int version = 0)
QByteArray | saveState(int version = 0) const
void | setCentralWidget(QWidget *widget)
void | setDockOptions(QMainWindow::DockOptions options)
void | setIconSize(const QSize &iconSize)
void | setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
QWidget * | takeCentralWidget()
Qt::ToolButtonStyle | toolButtonStyle() const

## Reimplemented Protected Functions

virtual void | contextMenuEvent(QContextMenuEvent *event) override
virtual bool | event(QEvent *event) override

## Detailed Description

### 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 QToolBars, QDockWidgets, 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 `setCorner()`. 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 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, and Menus Example.

## Member Type Documentation

### enum QMainWindow::DockOption
flags QMainWindow::DockOptions

This enum contains flags that specify the docking behavior of QMainWindow.

Constant | Value | Description
--- | --- | ---
`QMainWindow::AnimatedDocks` | `0x01` | Identical to the animated property.
`QMainWindow::AllowNestedDocks` | `0x02` | Identical to the dockNestingEnabled property.
`QMainWindow::AllowTabbedDocks` | `0x04` | The user can drop one dock widget "on top" of another. The two widgets are stacked and a tab bar appears for selecting which one is visible.
`QMainWindow::ForceTabbedDocks` | `0x08` | Each dock area contains a single stack of tabbed dock widgets. In other words, dock widgets cannot be placed next to each other in a dock area. If this option is set, AllowNestedDocks has no effect.
`QMainWindow::VerticalTabs` | `0x10` | The two vertical dock areas on the sides of the main window show their tabs vertically. If this option is not set, all dock areas show their tabs at the bottom. Implies AllowTabbedDocks. See also setTabPosition().
`QMainWindow::GroupedDragging` | `0x20` | When dragging the titlebar of a dock, all the tabs that are tabbed with it are going to be dragged. Implies AllowTabbedDocks. Does not work well if some QDockWidgets have restrictions in which area they are allowed. (This enum value was added in Qt 5.6.)

These options only control how dock widgets may be dropped in a QMainWindow. They do not re-arrange the dock widgets to conform with the specified options. For this reason they should be set before any dock widgets are added to the main window. Exceptions to this are the AnimatedDocks and VerticalTabs options, which may be set at any time.

The DockOptions type is a typedef for QFlags<DockOption>. It stores an OR combination of DockOption values.

## Property Documentation

### dockOptions : DockOptions

This property holds the docking behavior of QMainWindow

The default value is AnimatedDocks | AllowTabbedDocks.

Access functions:

QMainWindow::DockOptions | dockOptions() const
void | setDockOptions(QMainWindow::DockOptions options)

### iconSize : QSize

size of toolbar icons in this mainwindow.

The default is the default tool bar icon size of the GUI style. Note that the icons used must be at least of this size as the icons are only scaled down.

Access functions:

QSize | iconSize() const
void | setIconSize(const QSize &iconSize)

### toolButtonStyle : Qt::ToolButtonStyle

style of toolbar buttons in this mainwindow.

To have the style of toolbuttons follow the system settings, set this property to Qt::ToolButtonFollowStyle. On Unix, the user settings from the desktop environment will be used. On other platforms, Qt::ToolButtonFollowStyle means icon only.

The default is Qt::ToolButtonIconOnly.

Access functions:

Qt::ToolButtonStyle | toolButtonStyle() const
void | setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)

## Member Function Documentation

### `[explicit]` QMainWindow::QMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

Constructs a QMainWindow with the given parent and the specified widget flags.

QMainWindow sets the Qt::Window flag itself, and will hence always be created as a top-level widget.

### `[virtual noexcept]` QMainWindow::~QMainWindow()

Destroys the main window.

### QWidget *QMainWindow::centralWidget() const

Returns the central widget for the main window. This function returns `nullptr` if the central widget has not been set.

See also setCentralWidget().

### `[override virtual protected]` void QMainWindow::contextMenuEvent(QContextMenuEvent *event)

Reimplements: QWidget::contextMenuEvent(QContextMenuEvent *event).

### `[override virtual protected]` bool QMainWindow::event(QEvent *event)

Reimplements: QWidget::event(QEvent *event).

### bool QMainWindow::restoreState(const QByteArray &state, int version = 0)

Restores the state of this mainwindow's toolbars and dockwidgets. Also restores the corner settings too. The version number is compared with that stored in state. If they do not match, the mainwindow's state is left unchanged, and this function returns `false`; otherwise, the state is restored, and this function returns `true`.

To restore geometry saved using QSettings, you can use code like this:

     void MainWindow::readSettings()
     {
         QSettings settings("MyCompany", "MyApp");
         restoreGeometry(settings.value("myWidget/geometry").toByteArray());
         restoreState(settings.value("myWidget/windowState").toByteArray());
     }

See also saveState(), QWidget::saveGeometry(), QWidget::restoreGeometry(), and restoreDockWidget().

### QByteArray QMainWindow::saveState(int version = 0) const

Saves the current state of this mainwindow's toolbars and dockwidgets. This includes the corner settings which can be set with setCorner(). The version number is stored as part of the data.

The objectName property is used to identify each QToolBar and QDockWidget. You should make sure that this property is unique for each QToolBar and QDockWidget you add to the QMainWindow

To restore the saved state, pass the return value and version number to restoreState().

To save the geometry when the window closes, you can implement a close event like this:

     void MyMainWindow::closeEvent(QCloseEvent *event)
     {
         QSettings settings("MyCompany", "MyApp");
         settings.setValue("geometry", saveGeometry());
         settings.setValue("windowState", saveState());
         QMainWindow::closeEvent(event);
     }

See also restoreState(), QWidget::saveGeometry(), and QWidget::restoreGeometry().

### void QMainWindow::setCentralWidget(QWidget *widget)

Sets the given widget to be the main window's central widget.

Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.

See also centralWidget().

### QWidget *QMainWindow::takeCentralWidget()

Removes the central widget from this main window.

The ownership of the removed widget is passed to the caller.