6.0.0-rc4Source

QMainWindow Class

The QMainWindow class provides the main window of an application. More…

Header#include <QMainWindow>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Functions

Reimplemented Public Functions

Public Slots

Signals

Detailed Description

A main window has a menu bar at the top, tool bars under it and at the sides, dock widgets around a central widget, and a status bar at the bottom. All are optional except the central widget, which fills the space that is left:

QMainWindow window;
window.setCentralWidget(new QPlainTextEdit);
QMenu *file = window.menuBar()->addMenu("&File");
file->addAction("&Quit", [] { qApp->quit(); });
QToolBar *tools = window.addToolBar("Tools");
tools->addAction("Bold");
window.statusBar()->showMessage("Ready");
window.show();

menuBar() and statusBar() create the bars the first time they are called. Tool bars go in rows across the top and the bottom and in columns at the sides; addToolBarBreak() starts a new row. Dock widgets at the top and the bottom span the width of the window; the ones at the left and the right are between them, and several in one area share it.

createPopupMenu() returns a menu with the toggle view actions of the dock widgets and tool bars.

See also QMenuBar, QToolBar, QDockWidget and QStatusBar.

Differences from Qt

The user cannot drag tool bars or dock widgets to other areas or resize the dock areas. Several dock widgets in one area share it equally, side by side or one above the other; there are no tabbed dock widgets, no splitDockWidget(), tabifyDockWidget(), setCorner(), saveState() or restoreState(), and no animation. The main window does not show createPopupMenu() on a right click.

Member Function Documentation

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

Constructs a main window with the given parent and window flags. A main window is always a window, even with a parent.

[override] QMainWindow::~QMainWindow()

Destroys the main window and its bars and widgets.

void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget)

Adds dockwidget to the area of the window, after the dock widgets there. A floating dock widget is docked, and one that is in the window already moves. Emits QDockWidget::dockLocationChanged().

See also removeDockWidget() and dockWidgetArea().

void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget, Qt::Orientation orientation)

This is an overloaded function.

Adds dockwidget to area; orientation is accepted for compatibility: the dock widgets of the left and right areas are always one above the other, and those of the top and bottom areas side by side.

void QMainWindow::addToolBar(Qt::ToolBarArea area, QToolBar *toolbar)

Adds toolbar to the area of the window, after the tool bars there. A tool bar that is in the window already moves.

See also removeToolBar() and toolBarArea().

void QMainWindow::addToolBar(QToolBar *toolbar)

This is an overloaded function.

Adds toolbar at the top of the window.

QToolBar * QMainWindow::addToolBar(const QString &title)

This is an overloaded function.

Creates a tool bar with the given window title, adds it at the top of the window and returns it.

void QMainWindow::addToolBarBreak(Qt::ToolBarArea area = Qt::TopToolBarArea)

Makes the next tool bar added to area start a new line.

See also insertToolBarBreak() and toolBarBreak().

QWidget * QMainWindow::centralWidget() const

Returns the central widget of the window, or nullptr.

See also setCentralWidget().

[virtual] QMenu * QMainWindow::createPopupMenu()

Returns a new menu with the toggle view actions of the dock widgets and, after a separator, of the tool bars of the window, or nullptr if it has neither. The caller owns the menu.

See also QToolBar::toggleViewAction() and QDockWidget::toggleViewAction().

Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget *dockwidget) const

Returns the area that dockwidget is in, or Qt::NoDockWidgetArea if it is not in the window.

void QMainWindow::insertToolBar(QToolBar *before, QToolBar *toolbar)

Inserts toolbar before the tool bar before, in its area.

See also addToolBar().

void QMainWindow::insertToolBarBreak(QToolBar *before)

Makes the tool bar before start a new line of its area.

See also removeToolBarBreak().

Returns the menu bar of the window, creating it the first time if the window has no menu widget.

See also setMenuBar() and menuWidget().

Returns the widget at the top of the window, usually the menu bar, or nullptr.

See also setMenuWidget().

[override] QSize QMainWindow::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

void QMainWindow::removeDockWidget(QDockWidget *dockwidget)

Removes dockwidget from the window and hides it. The dock widget is not deleted.

See also addDockWidget().

void QMainWindow::removeToolBar(QToolBar *toolbar)

Removes toolbar from the window and hides it. The tool bar is not deleted.

See also addToolBar().

void QMainWindow::removeToolBarBreak(QToolBar *before)

Lets the tool bar before go on the line of the tool bar before it.

See also insertToolBarBreak().

bool QMainWindow::restoreDockWidget(QDockWidget *dockwidget)

Shows dockwidget again in its area if it is in the window, and returns whether it is.

void QMainWindow::setCentralWidget(QWidget *widget)

Makes widget the central widget, which fills the space that the bars and dock widgets leave. The window takes it as a child and deletes the central widget it had.

See also centralWidget() and takeCentralWidget().

void QMainWindow::setMenuBar(QMenuBar *menubar)

Makes menubar the menu bar of the window. The window takes it as a child and deletes the menu widget it had.

See also menuBar().

void QMainWindow::setMenuWidget(QWidget *menubar)

Makes menubar, which may be any widget, the widget at the top of the window. The window takes it as a child and deletes the one it had.

See also menuWidget() and setMenuBar().

void QMainWindow::setStatusBar(QStatusBar *statusbar)

Makes statusbar the status bar of the window, or removes the status bar with nullptr. The window takes it as a child and deletes the one it had.

See also statusBar().

[slot] void QMainWindow::setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)

Sets the style of the tool buttons of the tool bars of the window to toolButtonStyle.

See also toolButtonStyle().

[override] QSize QMainWindow::sizeHint() const

Reimplements QWidget::sizeHint().

QStatusBar * QMainWindow::statusBar() const

Returns the status bar of the window, creating it the first time.

See also setStatusBar().

QWidget * QMainWindow::takeCentralWidget()

Removes the central widget from the window without deleting it and returns it; the widget no longer has a parent, and the caller owns it.

See also setCentralWidget().

Qt::ToolBarArea QMainWindow::toolBarArea(const QToolBar *toolbar) const

Returns the area that toolbar is in, or Qt::NoToolBarArea if it is not in the window.

bool QMainWindow::toolBarBreak(QToolBar *toolbar) const

Returns whether a new line of tool bars starts with toolbar.

See also insertToolBarBreak().

Qt::ToolButtonStyle QMainWindow::toolButtonStyle() const

Returns the style of the tool buttons of the tool bars of the window, Qt::ToolButtonIconOnly by default.

See also setToolButtonStyle().

[signal] void QMainWindow::toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)

This signal is emitted when the style of the tool buttons changes to toolButtonStyle.