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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QMenuBar Class Reference

The QMenuBar class provides a horizontal menu bar. More...

 #include <QMenuBar>

Inherits: QWidget.

Properties

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

Public Functions

QMenuBar ( QWidget * parent = 0 )
~QMenuBar ()
QAction * actionAt ( const QPoint & pt ) const
QRect actionGeometry ( QAction * act ) const
QAction * activeAction () const
QAction * addAction ( const QString & text )
QAction * addAction ( const QString & text, const QObject * receiver, const char * member )
void addAction ( QAction * action )
QAction * addMenu ( QMenu * menu )
QMenu * addMenu ( const QString & title )
QMenu * addMenu ( const QIcon & icon, const QString & title )
QAction * addSeparator ()
void clear ()
QWidget * cornerWidget ( Qt::Corner corner = Qt::TopRightCorner ) const
QAction * defaultAction () const
QAction * insertMenu ( QAction * before, QMenu * menu )
QAction * insertSeparator ( QAction * before )
bool isDefaultUp () const
bool isNativeMenuBar () const
void setActiveAction ( QAction * act )
void setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )
void setDefaultAction ( QAction * act )
void setDefaultUp ( bool )
void setNativeMenuBar ( bool nativeMenuBar )

Reimplemented Public Functions

virtual int heightForWidth ( int ) const
virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const
  • 217 public functions inherited from QWidget
  • 29 public functions inherited from QObject
  • 13 public functions inherited from QPaintDevice

Public Slots

virtual void setVisible ( bool visible )
  • 19 public slots inherited from QWidget
  • 1 public slot inherited from QObject

Signals

void hovered ( QAction * action )
void triggered ( QAction * action )

Protected Functions

void initStyleOption ( QStyleOptionMenuItem * option, const QAction * action ) const

Reimplemented Protected Functions

virtual void actionEvent ( QActionEvent * e )
virtual void changeEvent ( QEvent * e )
virtual bool event ( QEvent * e )
virtual bool eventFilter ( QObject * object, QEvent * event )
virtual void focusInEvent ( QFocusEvent * )
virtual void focusOutEvent ( QFocusEvent * )
virtual void keyPressEvent ( QKeyEvent * e )
virtual void leaveEvent ( QEvent * )
virtual void mouseMoveEvent ( QMouseEvent * e )
virtual void mousePressEvent ( QMouseEvent * e )
virtual void mouseReleaseEvent ( QMouseEvent * e )
virtual void paintEvent ( QPaintEvent * e )
virtual void resizeEvent ( QResizeEvent * )
virtual void timerEvent ( QTimerEvent * e )
  • 37 protected functions inherited from QWidget
  • 7 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice

Additional Inherited Members

  • 4 static public members inherited from QWidget
  • 5 static public members inherited from QObject
  • 1 protected slot inherited from QWidget

Detailed Description

The QMenuBar class provides a horizontal menu bar.

A menu bar consists of a list of pull-down menu items. You add menu items with addMenu(). For example, asuming that menubar is a pointer to a QMenuBar and fileMenu is a pointer to a QMenu, the following statement inserts the menu into the menu bar:

 menubar->addMenu(fileMenu);

The ampersand in the menu item's text sets Alt+F as a shortcut for this menu. (You can use "&&" to get a real ampersand in the menu bar.)

There is no need to lay out a menu bar. It automatically sets its own geometry to the top of the parent widget and changes it appropriately whenever the parent is resized.

Usage

In most main window style applications you would use the menuBar() function provided in QMainWindow, adding QMenus to the menu bar and adding QActions to the pop-up menus.

Example (from the Menus example):

     fileMenu = menuBar()->addMenu(tr("&File"));
     fileMenu->addAction(newAct);

Menu items may be removed with removeAction().

Widgets can be added to menus by using instances of the QWidgetAction class to hold them. These actions can then be inserted into menus in the usual way; see the QMenu documentation for more details.

Platform Dependent Look and Feel

Different platforms have different requirements for the appearance of menu bars and their behavior when the user interacts with them. For example, Windows systems are often configured so that the underlined character mnemonics that indicate keyboard shortcuts for items in the menu bar are only shown when the Alt key is pressed.

A menu bar shown in the Plastique widget style.The Plastique widget style, like most other styles, handles the Help menu in the same way as it handles any other menu.
A menu bar shown in the Motif widget style.The Motif widget style treats Help menus in a special way, placing them at right-hand end of the menu bar.

QMenuBar on Mac OS X

QMenuBar on Mac OS X is a wrapper for using the system-wide menu bar. If you have multiple menu bars in one dialog the outermost menu bar (normally inside a widget with widget flag Qt::Window) will be used for the system-wide menu bar.

Qt for Mac OS X also provides a menu bar merging feature to make QMenuBar conform more closely to accepted Mac OS X menu bar layout. The merging functionality is based on string matching the title of a QMenu entry. These strings are translated (using QObject::tr()) in the "QMenuBar" context. If an entry is moved its slots will still fire as if it was in the original place. The table below outlines the strings looked for and where the entry is placed if matched:

String matchesPlacementNotes
about.*Application Menu | About <application name>The application name is fetched from the Info.plist file (see note below). If this entry is not found no About item will appear in the Application Menu.
config, options, setup, settings or preferencesApplication Menu | PreferencesIf this entry is not found the Settings item will be disabled
quit or exitApplication Menu | Quit <application name>If this entry is not found a default Quit item will be created to call QApplication::quit()

You can override this behavior by using the QAction::menuRole() property.

If you want all windows in a Mac application to share one menu bar, you must create a menu bar that does not have a parent. Create a parent-less menu bar this way:

 QMenuBar *menuBar = new QMenuBar(0);

Note: Do not call QMainWindow::menuBar() to create the shared menu bar, because that menu bar will have the QMainWindow as its parent. That menu bar would only be displayed for the parent QMainWindow.

Note: The text used for the application name in the menu bar is obtained from the value set in the Info.plist file in the application's bundle. See Deploying an Application on Mac OS X for more information.

QMenuBar on Windows CE

QMenuBar on Windows CE is a wrapper for using the system-wide menu bar, similar to the Mac. This feature is activated for Windows Mobile and integrates QMenuBar with the native soft keys. The left soft key can be controlled with QMenuBar::setDefaultAction() and the right soft key can be used to access the menu bar.

The hovered() signal is not supported for the native menu integration. Also, it is not possible to display an icon in a native menu on Windows Mobile.

Examples

The Menus example shows how to use QMenuBar and QMenu. The other main window application examples also provide menus using these classes.

See also QMenu, QShortcut, QAction, Introduction to Apple Human Interface Guidelines, GUI Design Handbook: Menu Bar, and Menus Example.

Property Documentation

defaultUp : bool

This property holds the popup orientation.

The default popup orientation. By default, menus pop "down" the screen. By setting the property to true, the menu will pop "up". You might call this for menus that are below the document to which they refer.

If the menu would not fit on the screen, the other direction is used automatically.

Access functions:

bool isDefaultUp () const
void setDefaultUp ( bool )

nativeMenuBar : bool

This property holds whether or not a menubar will be used as a native menubar on platforms that support it.

This property specifies whether or not the menubar should be used as a native menubar on platforms that support it. The currently supported platforms are Mac OS X and Windows CE. On these platforms if this property is true, the menubar is used in the native menubar and is not in the window of its parent, if false the menubar remains in the window. On other platforms the value of this attribute has no effect.

The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute is set for the application. Explicitly settings this property overrides the presence (or abscence) of the attribute.

This property was introduced in Qt 4.6.

Access functions:

bool isNativeMenuBar () const
void setNativeMenuBar ( bool nativeMenuBar )

Member Function Documentation

QMenuBar::QMenuBar ( QWidget * parent = 0 )

Constructs a menu bar with parent parent.

QMenuBar::~QMenuBar ()

Destroys the menu bar.

QAction * QMenuBar::actionAt ( const QPoint & pt ) const

Returns the QAction at pt. Returns 0 if there is no action at pt or if the location has a separator.

See also addAction() and addSeparator().

void QMenuBar::actionEvent ( QActionEvent * e ) [virtual protected]

Reimplemented from QWidget::actionEvent().

QRect QMenuBar::actionGeometry ( QAction * act ) const

Returns the geometry of action act as a QRect.

See also actionAt().

QAction * QMenuBar::activeAction () const

Returns the QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.

See also setActiveAction().

QAction * QMenuBar::addAction ( const QString & text )

This is an overloaded function.

This convenience function creates a new action with text. The function adds the newly created action to the menu's list of actions, and returns it.

See also QWidget::addAction() and QWidget::actions().

QAction * QMenuBar::addAction ( const QString & text, const QObject * receiver, const char * member )

This is an overloaded function.

This convenience function creates a new action with the given text. The action's triggered() signal is connected to the receiver's member slot. The function adds the newly created action to the menu's list of actions and returns it.

See also QWidget::addAction() and QWidget::actions().

void QMenuBar::addAction ( QAction * action )

This is an overloaded function.

Appends the action action to the menu bar's list of actions.

See also QMenu::addAction(), QWidget::addAction(), and QWidget::actions().

QAction * QMenuBar::addMenu ( QMenu * menu )

Appends menu to the menu bar. Returns the menu's menuAction().

Note: The returned QAction object can be used to hide the corresponding menu.

See also QWidget::addAction() and QMenu::menuAction().

QMenu * QMenuBar::addMenu ( const QString & title )

Appends a new QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

See also QWidget::addAction() and QMenu::menuAction().

QMenu * QMenuBar::addMenu ( const QIcon & icon, const QString & title )

Appends a new QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

See also QWidget::addAction() and QMenu::menuAction().

QAction * QMenuBar::addSeparator ()

Appends a separator to the menu.

void QMenuBar::changeEvent ( QEvent * e ) [virtual protected]

Reimplemented from QWidget::changeEvent().

void QMenuBar::clear ()

Removes all the actions from the menu bar.

Note: On Mac OS X, menu items that have been merged to the system menu bar are not removed by this function. One way to handle this would be to remove the extra actions yourself. You can set the menu role on the different menus, so that you know ahead of time which menu items get merged and which do not. Then decide what to recreate or remove yourself.

See also removeAction().

QWidget * QMenuBar::cornerWidget ( Qt::Corner corner = Qt::TopRightCorner ) const

Returns the widget on the left of the first or on the right of the last menu item, depending on corner.

Note: Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.

See also setCornerWidget().

QAction * QMenuBar::defaultAction () const

Returns the current default action.

This function was introduced in Qt 4.4.

See also setDefaultAction().

bool QMenuBar::event ( QEvent * e ) [virtual protected]

Reimplemented from QObject::event().

bool QMenuBar::eventFilter ( QObject * object, QEvent * event ) [virtual protected]

Reimplemented from QObject::eventFilter().

void QMenuBar::focusInEvent ( QFocusEvent * ) [virtual protected]

Reimplemented from QWidget::focusInEvent().

void QMenuBar::focusOutEvent ( QFocusEvent * ) [virtual protected]

Reimplemented from QWidget::focusOutEvent().

int QMenuBar::heightForWidth ( int ) const [virtual]

Reimplemented from QWidget::heightForWidth().

void QMenuBar::hovered ( QAction * action ) [signal]

This signal is emitted when a menu action is highlighted; action is the action that caused the event to be sent.

Often this is used to update status information.

See also triggered() and QAction::hovered().

void QMenuBar::initStyleOption ( QStyleOptionMenuItem * option, const QAction * action ) const [protected]

Initialize option with the values from the menu bar and information from action. This method is useful for subclasses when they need a QStyleOptionMenuItem, but don't want to fill in all the information themselves.

See also QStyleOption::initFrom() and QMenu::initStyleOption().

QAction * QMenuBar::insertMenu ( QAction * before, QMenu * menu )

This convenience function inserts menu before action before and returns the menus menuAction().

See also QWidget::insertAction() and addMenu().

QAction * QMenuBar::insertSeparator ( QAction * before )

This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true. The function inserts the newly created action into this menu bar's list of actions before action before and returns it.

See also QWidget::insertAction() and addSeparator().

void QMenuBar::keyPressEvent ( QKeyEvent * e ) [virtual protected]

Reimplemented from QWidget::keyPressEvent().

void QMenuBar::leaveEvent ( QEvent * ) [virtual protected]

Reimplemented from QWidget::leaveEvent().

QSize QMenuBar::minimumSizeHint () const [virtual]

Reimplemented from QWidget::minimumSizeHint().

void QMenuBar::mouseMoveEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented from QWidget::mouseMoveEvent().

void QMenuBar::mousePressEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented from QWidget::mousePressEvent().

void QMenuBar::mouseReleaseEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented from QWidget::mouseReleaseEvent().

void QMenuBar::paintEvent ( QPaintEvent * e ) [virtual protected]

Reimplemented from QWidget::paintEvent().

void QMenuBar::resizeEvent ( QResizeEvent * ) [virtual protected]

Reimplemented from QWidget::resizeEvent().

void QMenuBar::setActiveAction ( QAction * act )

Sets the currently highlighted action to act.

This function was introduced in Qt 4.1.

See also activeAction().

void QMenuBar::setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )

This sets the given widget to be shown directly on the left of the first menu item, or on the right of the last menu item, depending on corner.

The menu bar takes ownership of widget, reparenting it into the menu bar. However, if the corner already contains a widget, this previous widget will no longer be managed and will still be a visible child of the menu bar.

Note: Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.

See also cornerWidget().

void QMenuBar::setDefaultAction ( QAction * act )

Sets the default action to act.

The default action is assigned to the left soft key. The menu is assigned to the right soft key.

Currently there is only support for the default action on Windows Mobile. On all other platforms this method is not available.

This function was introduced in Qt 4.4.

See also defaultAction().

void QMenuBar::setVisible ( bool visible ) [virtual slot]

Reimplemented from QWidget::setVisible().

QSize QMenuBar::sizeHint () const [virtual]

Reimplemented from QWidget::sizeHint().

void QMenuBar::timerEvent ( QTimerEvent * e ) [virtual protected]

Reimplemented from QObject::timerEvent().

void QMenuBar::triggered ( QAction * action ) [signal]

This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of a mouse click; action is the action that caused the signal to be emitted.

Normally, you connect each menu action to a single slot using QAction::triggered(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is useful in such cases.

See also hovered() and QAction::triggered().

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 blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. 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.7
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