Qt Toolkit - QMainWindow Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QMainWindow Class Reference

The QMainWindow class provides a typical application window, with a menu bar, some tool bars and a status bar. More...

`#include <qmainwindow.h>`

Inherits QWidget.

List of all member functions.

## Public Members

- QMainWindow ( QWidget * parent = 0, const char * name = 0, WFlags f = WType_TopLevel )
- ~QMainWindow ()
- QMenuBar* menuBar () const
- QStatusBar* statusBar () const
- QToolTipGroup* toolTipGroup () const
- virtual void setCentralWidget ( QWidget * )
- QWidget* centralWidget () const
- enum ToolBarDock { Unmanaged, TornOff, Top, Bottom, Right, Left, Minimized }
- virtual void setDockEnabled ( ToolBarDock dock, bool enable )
- bool isDockEnabled ( ToolBarDock dock ) const
- void setDockEnabled ( QToolBar * tb, ToolBarDock dock, bool enable )
- bool isDockEnabled ( QToolBar * tb, ToolBarDock dock ) const
- void addToolBar ( QToolBar *, ToolBarDock = Top, bool newLine = FALSE )
- void addToolBar ( QToolBar *, const QString & label, ToolBarDock = Top, bool newLine = FALSE )
- void moveToolBar ( QToolBar *, ToolBarDock = Top )
- void moveToolBar ( QToolBar *, ToolBarDock, bool nl, int index, int extraOffset = -1 )
- void removeToolBar ( QToolBar * )
- bool usesBigPixmaps () const
- bool usesTextLabel () const
- bool rightJustification () const
- bool toolBarsMovable () const
- bool opaqueMoving () const
- bool getLocation ( QToolBar * tb, ToolBarDock & dock, int & index, bool & nl, int & extraOffset ) const
- void lineUpToolBars ( bool keepNewLines = FALSE )
- bool isDockMenuEnabled () const

## Public Slots

- virtual void setRightJustification ( bool )
- void setOpaqueMoving ( bool )
- void setToolBarsMovable ( bool )
- void setDockMenuEnabled ( bool )
- virtual void setUsesBigPixmaps ( bool )
- void setUsesTextLabel ( bool )
- void whatsThis ()

## Signals

- void pixmapSizeChanged ( bool )
- void usesTextLabelChanged ( bool )
- void startMovingToolBar ( QToolBar * )
- void endMovingToolBar ( QToolBar * )
- void toolBarPositionChanged ( QToolBar * )

## Protected Members

- virtual void resizeEvent ( QResizeEvent * )
- virtual void childEvent ( QChildEvent * )

## Protected Slots

- virtual void setUpLayout ()

## Properties

Type | Name | READ | WRITE | Options
--- | --- | --- | --- | ---
bool | rightJustification | rightJustification | setRightJustification |
bool | usesBigPixmaps | usesBigPixmaps | setUsesBigPixmaps |
bool | usesTextLabel | usesTextLabel | setUsesTextLabel |
bool | toolBarsMovable | toolBarsMovable | setToolBarsMovable |
bool | opaqueMoving | opaqueMoving | setOpaqueMoving |

## Detailed Description

The QMainWindow class provides a typical application window, with a menu bar, some tool bars and a status bar.

In addition, you need the large central widget, which you supply and tell QMainWindow about using setCentralWidget(), and perhaps a few tool bars, which you can add using addToolBar().

The central widget is not touched by QMainWindow. QMainWindow manages its geometry, and that is all. For example, the application/application.cpp example (an editor) sets a QMultiLineEdit to be the central widget.

QMainWindow automatically detects the creation of a menu bar or status bar if you specify the QMainWindow as parent, or you can use the provided menuBar() and statusBar() functions. menuBar() and statusBar() create a suitable widget if one doesn't exist, and updates the window's layout to make space.

QMainWindow also provides a QToolTipGroup connected to the status bar. toolTipGroup() provides access to the QToolTipGroup, but there is no way to set the tool tip group.

The QMainWindow allows by default toolbars in all docking areas. You can use setDockEnabled() to enable and disable docking areas for toolbars. Currently, only `Top, Left, Right, Bottom` and `Minimized` are meaningful.

Several functions let you change the appearance of a QMainWindow globally:

- setRightJustification() determines whether QMainWindow should ensure that the toolbars fill the available space (see also QToolBar::setHorizontalStretchable() and QToolBar::setVerticalStretchable()),
- setUsesBigPixmaps() determines whether QToolButton (and other classes) should draw small or large pixmaps (see QIconSet for more about that),
- setUsesTextLabel() determines whether the toolbar buttons (and other classes), should display a textlabel in addition to pixmaps (see QToolButton for more about that).

Toolbars can be dragged by the user into each enabled docking area and inside each docking area to change the order of the toolbars there. This feature can be enabled and disabled using setToolBarsMovable(). By default this feature is enabled. If the `Minimized` dock is enabled the user can hide(minimize)/show(restore) a toolbar with a click on the toolbar handle. The handles of all minimized toolbars are drawn below the menu bar in one row, and if the user moves the mouse cursor onto such a handle, the label of the toolbar is displayed in a tool tip (see QToolBar::label()). So if you enable the Minimized dock, you should specify a meaningful label for each toolbar.

Normally toolbars are moved transparently (this means while the user drags one, a rectangle is drawn on the screen). With setOpaqueMoving() it's possible to switch between opaque and transparent moving of toolbars.

The main window's menubar is static (on the top) by default. If you want a movable menubar, create a QMenuBar as stretchable widget inside its own movable toolbar and restrict this toolbar to only live within the Top or Bottom dock:

      QToolBar *tb = new QToolBar( this );
      addToolBar( tb, tr( "Menubar" ), Top, FALSE );
      QMenuBar *mb = new QMenuBar( tb );
      mb->setFrameStyle( QFrame::NoFrame );
      tb->setStretchableWidget( mb );
      setDockEnabled( tb, Left, FALSE );
      setDockEnabled( tb, Right, FALSE );

An application with multiple toolbars can choose to save the current toolbar layout in order to restore it in the next session. To do so, use getLocation() on each toolbar, store the data and restore the layout using moveToolBar() on each toolbar again. When restoring, ensure to move the toolbars in exactly the same order in which you got the information.

For multi-document interfaces (MDI), use a QWorkspace as central widget.

See also QToolBar, QStatusBar, QMenuBar, QToolTipGroup and QDialog.

Examples: mainlyQt/editor.cpp

## Member Type Documentation

### QMainWindow::ToolBarDock

Each toolbar can be in one of the following positions:

- `Top` - above the central widget, below the menubar.
- `Bottom` - below the central widget, above the status bar.
- `Left` - to the left of the central widget.
- `Right` - to the left of the central widget.
- `Minimized` - the toolbar is not shown - all handles of minimized toolbars are drawn in one row below the menu bar.

Other values are also defined for future expansion.

## Member Function Documentation

### QMainWindow::QMainWindow ( QWidget * parent = 0, const char * name = 0, WFlags f = WType_TopLevel )

Constructs an empty main window.

### QMainWindow::~QMainWindow ()

Destructs the object and frees any allocated resources.

### void QMainWindow::addToolBar ( QToolBar * toolBar, ToolBarDock edge = Top, bool newLine = FALSE )

Adds toolBar to this the end of edge and makes it start a new line of tool bars if nl is TRUE.

If toolBar is already managed by some main window, it is first removed from that window.

### void QMainWindow::addToolBar ( QToolBar * toolBar, const QString & label, ToolBarDock edge = Top, bool newLine = FALSE )

Adds toolBar to this the end of edge, labelling it label and makes it start a new line of tool bars if newLine is TRUE.

If toolBar is already managed by some main window, it is first removed from that window.

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

Returns a pointer to the main child of this main widget. The main child is the big widget around which the tool bars are arranged.

See also setCentralWidget().

Examples: qfd/qfd.cpp

### void QMainWindow::childEvent ( QChildEvent * e ) `[virtual protected]`

Monitors events to ensure layout is updated.

Reimplemented from QObject.

### void QMainWindow::endMovingToolBar ( QToolBar * toolbar ) `[signal]`

This signal is emitted if the toolbar has been moved by the user and he/she released the mouse button now, so he/she stopped the moving.

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

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

### bool QMainWindow::eventFilter ( QObject * o, QEvent * e ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

### bool QMainWindow::getLocation ( QToolBar * tb, ToolBarDock & dock, int & index, bool & nl, int & extraOffset ) const

Finds and gives back the dock and the index there of the toolbar tb. dock is set to the dock of the mainwindow in which tb is and index is set to the position of the toolbar in this dock. If the toolbar has a new line, nl is set to TRUE, else to FALSE.

This method returns TRUE if the information could be found out, otherwise FALSE (e.g. because the toolbar tb was not found in this mainwindow).

### bool QMainWindow::isDockEnabled ( ToolBarDock dock ) const

Returns TRUE if dock is enabled, or FALSE if it is not.

See also setDockEnabled().

### bool QMainWindow::isDockEnabled ( QToolBar * tb, ToolBarDock dock ) const

Returns TRUE if dock is enabled for the toolbar tb , or FALSE if it is not.

See also setDockEnabled().

### bool QMainWindow::isDockMenuEnabled () const

Returns TRUE, if rightclicking on an empty space on a toolbar dock or rightclicking on a toolbar handle opens a popup menu which allows lining up toolbars and hiding/showing toolbars.

See also setDockEnabled() and lineUpToolBars().

### void QMainWindow::lineUpToolBars ( bool keepNewLines = FALSE )

As toolbars can be freely moved inside docks, it's possible to line them up nicely with this method to get rid of all the unused space. If keepNewLines is TRUE, all toolbars stay in the line in which they are, else they are packed together as compact as possible.

The method only works if movable() returns TRUE.

### QMenuBar * QMainWindow::menuBar () const

Returns the menu bar for this window. If there isn't any, menuBar() creates an empty menu bar on the fly.

See also statusBar().

### QSize QMainWindow::minimumSizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMainWindow::moveToolBar ( QToolBar * toolBar, ToolBarDock edge = Top )

Moves toolBar to this the end of edge.

If toolBar is already managed by some main window, it is moved from that window to this.

### void QMainWindow::moveToolBar ( QToolBar * toolBar, ToolBarDock edge, bool nl, int index, int extraOffset = -1 )

Moves toolBar to the position index of edge.

If toolBar is already managed by some main window, it is moved from that window to this.

### bool QMainWindow::opaqueMoving () const

Returns whether the toolbars of the mainwindow can be moved opaque or transparent.

See also setOpaqueMoving().

### void QMainWindow::paintEvent ( QPaintEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMainWindow::pixmapSizeChanged ( bool ) `[signal]`

This signal is called whenever the setUsesBigPixmaps() is called with a value which is different from the current setting. All relevant widgets must connect to this signal.

### void QMainWindow::removeToolBar ( QToolBar * toolBar )

Removes toolBar from this main window's docking area, if toolBar is non-null and known by this main window.

### void QMainWindow::resizeEvent ( QResizeEvent * ) `[virtual protected]`

Monitors events to ensure layout is updated.

Reimplemented from QWidget.

### bool QMainWindow::rightJustification () const

Returns TRUE if this main windows right-justifies its toolbars, and FALSE if it uses a ragged right edge.

The default is to use a ragged right edge.

("Right edge" sometimes means "bottom edge".)

See also setRightJustification().

### void QMainWindow::setCentralWidget ( QWidget * w ) `[virtual]`

Sets the central widget for this window to w. The central widget is the one around which the toolbars etc. are arranged.

### void QMainWindow::setDockEnabled ( ToolBarDock dock, bool enable )

Sets dock to be available if enable is TRUE, and not available if enable is FALSE.

The user can drag a toolbar to any enabled dock.

### void QMainWindow::setDockEnabled ( QToolBar * tb, ToolBarDock dock, bool enable )

Sets dock to be available for the toolbar tb if enable is TRUE, and not available if enable is FALSE.

The user can drag the toolbar to any enabled dock.

### void QMainWindow::setDockMenuEnabled ( bool b ) `[slot]`

When passing TRUE for b here, rightclicking on an empty space on a toolbar dock or rightclicking on a toolbar handle opens a popup menu which allows lining up toolbars and hiding/showing toolbars.

See also lineUpToolBars() and isDockMenuEnabled().

### void QMainWindow::setOpaqueMoving ( bool b ) `[slot]`

If you set b to TRUE, the use can move the toolbars opaque, otherwise this is done transparent. This setting makes only sense, if toolBarsMovable() is set to TRUE.

See also setToolbarsMovable().

### void QMainWindow::setRightJustification ( bool enable ) `[virtual slot]`

Sets this main window to right-justifies its toolbars if enable is TRUE. If enable is FALSE, only stretchable toolbars are expanded, while non-stretchable toolbars get just the space they need. Given that most toolbars are not stretchable, this usually results in a ragged right edge.

The default is FALSE.

See also rightJustification(), QToolBar::setVerticalStretchable() and QToolBar::setHorizontalStretchable().

### void QMainWindow::setToolBarsMovable ( bool enable ) `[slot]`

Sets the toolbars to be movable if enable is TRUE, or static otherwise.

Movable toolbars can be dragged around between and within the different toolbar docks by the user. By default toolbars are moved transparent, but this setting can be changed by setOpaqueMoving().

The default is TRUE.

See also setDockEnabled(), toolBarsMovable() and setOpaqueMoving().

### void QMainWindow::setUpLayout () `[virtual protected slot]`

Sets up the geometry management of this window. Called automatically when needed, so you should never need to call this.

### void QMainWindow::setUsesBigPixmaps ( bool enable ) `[virtual slot]`

Sets tool buttons in this main windows to use big pixmaps if enable is TRUE, and small pixmaps if enable is FALSE.

The default is FALSE.

Tool buttons and other interested widgets are responsible for reading the correct state on startup, and for connecting to this widget's pixmapSizeChanged() signal.

See also QToolButton::setUsesBigPixmap().

### void QMainWindow::setUsesTextLabel ( bool enable ) `[slot]`

Sets tool buttons in this main windows to use text labels if enable is TRUE, and no text labels otherwise.

The default is FALSE.

Tool buttons and other interested widgets are responsible for reading the correct state on startup, and for connecting to this widget's usesTextLabelChanged() signal.

See also QToolButton::setUsesTextLabel().

### void QMainWindow::show () `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Examples: action/main.cpp themes/main.cpp mainlyQt/editor.cpp application/main.cpp helpviewer/main.cpp i18n/main.cpp fileiconview/main.cpp scribble/main.cpp qfd/qfd.cpp addressbook/main.cpp mdi/main.cpp

Reimplemented from QWidget.

### QSize QMainWindow::sizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMainWindow::startMovingToolBar ( QToolBar * toolbar ) `[signal]`

This signal is emitted when the toolbar starts moving because the user started dragging it.

### QStatusBar * QMainWindow::statusBar () const

Returns the status bar for this window. If there isn't any, statusBar() creates an empty status bar on the fly, and if necessary a tool tip group too.

See also menuBar() and toolTipGroup().

Examples: qfd/qfd.cpp

### void QMainWindow::styleChange ( QStyle & old ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QMainWindow::toolBarPositionChanged ( QToolBar * toolbar ) `[signal]`

This signal is emitted when the toolbar has changed its position. This means it has been moved to another dock or inside the dock.

See also getLocation().

### bool QMainWindow::toolBarsMovable () const

Returns whether or not the toolbars of this main window are movable.

See also setToolBarsMovable().

### QToolTipGroup * QMainWindow::toolTipGroup () const

Returns the tool tip group for this window. If there isn't any, toolTipGroup() creates an empty tool tip group on the fly.

See also menuBar() and statusBar().

### bool QMainWindow::usesBigPixmaps () const

Returns the state last set by setUsesBigPixmaps(). The initial state is FALSE.

See also setUsesBigPixmaps();.

### bool QMainWindow::usesTextLabel () const

Returns the state last set by setUsesTextLabel(). The initial state is FALSE.

See also setUsesTextLabel();.

### void QMainWindow::usesTextLabelChanged ( bool ) `[signal]`

This signal is called whenever the setUsesTextLabel() is called with a value which is different from the current setting. All relevant widgets must connect to this signal.

### void QMainWindow::whatsThis () `[slot]`

Enters What's This? question mode and returns immediately.

This is the same as QWhatsThis::enterWhatsThisMode(), but as a slot of of a main window object. This way it can be easily used for popup menus as in the code fragment:

        QPopupMenu * help = new QPopupMenu( this );
        help->insertItem( "What's &This", this , SLOT(whatsThis()), SHIFT+Key_F1);

See also QWhatsThis::enterWhatsThisMode().

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.