Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QMainWindow Class Reference
[QtGui module]

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

 #include <QMainWindow>

Inherits QWidget.

Properties

  • 55 properties inherited from QWidget
  • 1 property inherited from QObject

Public Functions

  • 195 public functions inherited from QWidget
  • 29 public functions inherited from QObject
  • 12 public functions inherited from QPaintDevice

Public Slots

  • 19 public slots inherited from QWidget
  • 1 public slot inherited from QObject

Signals

Additional Inherited Members

  • 4 static public members inherited from QWidget
  • 5 static public members inherited from QObject
  • 38 protected functions inherited from QWidget
  • 7 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice
  • 1 protected slot inherited from QWidget

Detailed Description

The QMainWindow class provides a main application window.

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, QDockWindows, 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 QWorkspace 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.


Property Documentation

animated : bool

This property holds whether manipulating dock widgets is animated.

When a dock widget is dragged over the main window, other dock widgets in the window will adjust themselves to make space for the dragged widget. If this property is set to true, their movement will be animated. The default value is true.

This property was introduced in Qt 4.2.

Access functions:

  • bool isAnimated () const
  • void setAnimated ( bool enabled )

dockNestingEnabled : bool

This property holds whether docks can be nested.

If this property is set to false, dock areas can only contain a single row (horizontal or vertical) of dock widgets. If this property is set to true, the area occupied by a dock widget can be split in either direction to contain more dock widgets.

Dock nesting is only necessary in applications that contain a lot of dock widgets. It gives the user greater freedom in organizing their main window. However, dock nesting leads to more complex (and less intuitive) behavior when a dock widget is dragged over the main window, since there are more ways in which a dropped dock widget may be placed in the dock area.

This property was introduced in Qt 4.2.

Access functions:

  • bool isDockNestingEnabled () const
  • void setDockNestingEnabled ( bool enabled )

iconSize : QSize

This property holds size of toolbar icons in this mainwindow.

The default is the default tool bar icon size of the GUI style.

Access functions:

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

toolButtonStyle : Qt::ToolButtonStyle

This property holds style of toolbar buttons in this mainwindow.

The default is Qt::ToolButtonIconOnly.

Access functions:

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

Member Function Documentation

QMainWindow::QMainWindow ( QWidget * parent = 0, Qt::WindowFlags flags = 0 )

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

QMainWindow::~QMainWindow ()

Destroys the main window.

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

Adds the given dockwidget to the specified area.

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

This is an overloaded member function, provided for convenience.

Adds dockwidget into the given area in the direction specified by the orientation.

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

Adds the toolbar into the specified area in this main window. The toolbar is placed at the end of the current tool bar block (i.e. line). If the main window already manages toolbar then it will only move the toolbar to area.

See also insertToolBar(), addToolBarBreak(), and insertToolBarBreak().

void QMainWindow::addToolBar ( QToolBar * toolbar )

This is an overloaded member function, provided for convenience.

Equivalent of calling addToolBar(Qt::TopToolBarArea, toolbar)

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

This is an overloaded member function, provided for convenience.

Creates a QToolBar object, setting its window title to title, and inserts it into the top toolbar area.

See also setWindowTitle().

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

Adds a toolbar break to the given area after all the other objects that are present.

QWidget * QMainWindow::centralWidget () const

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

See also setCentralWidget().

Qt::DockWidgetArea QMainWindow::corner ( Qt::Corner corner ) const

Returns the dock widget area that occupies the specified corner.

See also setCorner().

QMenu * QMainWindow::createPopupMenu ()   [virtual]

Returns a popup menu containing checkable entries for the toolbars and dock widgets present in the main window.

By default, this function is called by the main window when the user activates a context menu, typically by right-clicking on a toolbar or a dock widget.

If you want to create a custom popup menu, reimplement this function and return a newly-created popup menu. Ownership of the popup menu is transferred to the caller.

See also addDockWidget(), addToolBar(), and menuBar().

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

Returns the Qt::DockWidgetArea for dockwidget. If dockwidget has not been added to the main window, this function returns Qt::NoDockWidgetArea.

See also addDockWidget(), splitDockWidget(), and Qt::DockWidgetArea.

void QMainWindow::iconSizeChanged ( const QSize & iconSize )   [signal]

This signal is emitted when the size of the icons used in the window is changed. The new icon size is passed in iconSize.

You can connect this signal to other components to help maintain a consistent appearance for your application.

See also setIconSize().

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

Inserts the toolbar into the area occupied by the before toolbar so that it appears before it. For example, in normal left-to-right layout operation, this means that toolbar will appear to the left of the toolbar specified by before in a horizontal toolbar area.

See also insertToolBarBreak(), addToolBar(), and addToolBarBreak().

void QMainWindow::insertToolBarBreak ( QToolBar * before )

Inserts a toolbar break before the toolbar specified by before.

QMenuBar * QMainWindow::menuBar () const

Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.

See also setMenuBar().

QWidget * QMainWindow::menuWidget () const

Returns the menu bar for the main window. This function returns null if a menubar hasn't been constructed yet.

This function was introduced in Qt 4.2.

See also setMenuWidget().

void QMainWindow::removeDockWidget ( QDockWidget * dockwidget )

Removes the dockwidget from the main window layout and hides it. Note that the dockwidget is not deleted.

void QMainWindow::removeToolBar ( QToolBar * toolbar )

Removes the toolbar from the main window layout and hides it. Note that the toolbar is not deleted.

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

Restores the state of this mainwindow's toolbars and dockwidgets. 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.

See also saveState().

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

Saves the current state of this mainwindow's toolbars and dockwidgets. 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().

See also restoreState().

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().

void QMainWindow::setCorner ( Qt::Corner corner, Qt::DockWidgetArea area )

Sets the given dock widget area to occupy the specified corner.

See also corner().

void QMainWindow::setMenuBar ( QMenuBar * menuBar )

Sets the menu bar for the main window to menuBar.

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

See also menuBar().

void QMainWindow::setMenuWidget ( QWidget * menuBar )

Sets the menu bar for the main window to menuBar.

QMainWindow takes ownership of the menuBar pointer and deletes it at the appropriate time.

This function was introduced in Qt 4.2.

See also menuWidget().

void QMainWindow::setStatusBar ( QStatusBar * statusbar )

Sets the status bar for the main window to statusbar.

Setting the status bar to 0 will remove it from the main window. Note that QMainWindow takes ownership of the statusbar pointer and deletes it at the appropriate time.

See also statusBar().

void QMainWindow::splitDockWidget ( QDockWidget * first, QDockWidget * second, Qt::Orientation orientation )

Splits the space covered by the first dock widget into two parts, moves the first dock widget into the first part, and moves the second dock widget into the second part.

The orientation specifies how the space is divided: A Qt::Horizontal split places the second dock widget to the right of the first; a Qt::Vertical split places the second dock widget below the first.

Note: if first is currently in a tabbed docked area, second will be added as a new tab, not as a neighbor of first. This is because a single tab can contain only one dock widget.

Note: The Qt::LayoutDirection influences the order of the dock widgets in the two parts of the divided area. When right-to-left layout direction is enabled, the placing of the dock widgets will be reversed.

See also tabifyDockWidget(), addDockWidget(), and removeDockWidget().

QStatusBar * QMainWindow::statusBar () const

Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist.

See also setStatusBar().

void QMainWindow::tabifyDockWidget ( QDockWidget * first, QDockWidget * second )

Moves second dock widget on top of first dock widget, creating a tabbed docked area in the main window.

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

Returns the Qt::ToolBarArea for toolbar. If toolbar has not been added to the main window, this function returns Qt::NoToolBarArea.

See also addToolBar(), addToolBarBreak(), and Qt::ToolBarArea.

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

This signal is emitted when the style used for tool buttons in the window is changed. The new style is passed in toolButtonStyle.

You can connect this signal to other components to help maintain a consistent appearance for your application.

See also setToolButtonStyle().


Member Function Documentation

QMainWindow::QMainWindow ( QWidget * parent, const char * name, Qt::WindowFlags flags = 0 )

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

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.2
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web