Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QMenuBar Class

TheQMenuBar class provides a horizontal menu bar.More...

Header:#include <QMenuBar>
Inherits:QWidget

Properties

Public Functions

QMenuBar(QWidget * parent = 0)
~QMenuBar()
QAction *actionAt(const QPoint & pt) const
QRectactionGeometry(QAction * act) const
QAction *activeAction() const
QAction *addAction(const QString & text)
QAction *addAction(const QString & text, const QObject * receiver, const char * member)
voidaddAction(QAction * action)
QAction *addMenu(QMenu * menu)
QMenu *addMenu(const QString & title)
QMenu *addMenu(const QIcon & icon, const QString & title)
QAction *addSeparator()
voidclear()
QWidget *cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const
QAction *defaultAction() const
QAction *insertMenu(QAction * before, QMenu * menu)
QAction *insertSeparator(QAction * before)
boolisDefaultUp() const
boolisNativeMenuBar() const
voidsetActiveAction(QAction * act)
voidsetCornerWidget(QWidget * widget, Qt::Corner corner = Qt::TopRightCorner)
voidsetDefaultAction(QAction * act)
voidsetDefaultUp(bool)
voidsetNativeMenuBar(bool nativeMenuBar)

Reimplemented Public Functions

virtual intheightForWidth(int) const
virtual QSizeminimumSizeHint() const
virtual QSizesizeHint() const

Public Slots

virtual voidsetVisible(bool visible)
  • 19 public slots inherited fromQWidget
  • 1 public slot inherited fromQObject

Signals

voidhovered(QAction * action)
voidtriggered(QAction * action)

Protected Functions

voidinitStyleOption(QStyleOptionMenuItem * option, const QAction * action) const

Reimplemented Protected Functions

virtual voidactionEvent(QActionEvent * e)
virtual voidchangeEvent(QEvent * e)
virtual boolevent(QEvent * e)
virtual booleventFilter(QObject * object, QEvent * event)
virtual voidfocusInEvent(QFocusEvent *)
virtual voidfocusOutEvent(QFocusEvent *)
virtual voidkeyPressEvent(QKeyEvent * e)
virtual voidleaveEvent(QEvent *)
virtual voidmouseMoveEvent(QMouseEvent * e)
virtual voidmousePressEvent(QMouseEvent * e)
virtual voidmouseReleaseEvent(QMouseEvent * e)
virtual voidpaintEvent(QPaintEvent * e)
virtual voidresizeEvent(QResizeEvent *)
virtual voidtimerEvent(QTimerEvent * e)
  • 37 protected functions inherited fromQWidget
  • 8 protected functions inherited fromQObject
  • 1 protected function inherited fromQPaintDevice

Additional Inherited Members

  • 4 static public members inherited fromQWidget
  • 7 static public members inherited fromQObject
  • 1 protected slot inherited fromQWidget

Detailed Description

TheQMenuBar class provides a horizontal menu bar.

A menu bar consists of a list of pull-down menu items. You add menu items withaddMenu(). For example, asuming thatmenubar is a pointer to aQMenuBar andfileMenu is a pointer to aQMenu, 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 themenuBar() function provided inQMainWindow, addingQMenus to the menu bar and addingQActions to the pop-up menus.

Example (from theMenus example):

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

Menu items may be removed withremoveAction().

Widgets can be added to menus by using instances of theQWidgetAction class to hold them. These actions can then be inserted into menus in the usual way; see theQMenu 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 theAlt key is pressed.

A menu bar shown in the Plastique widget style.ThePlastique widget style, like most other styles, handles theHelp menu in the same way as it handles any other menu.
A menu bar shown in the Motif widget style.TheMotif widget style treatsHelp 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 flagQt::Window) will be used for the system-wide menu bar.

Qt for Mac OS X also provides a menu bar merging feature to makeQMenuBar conform more closely to accepted Mac OS X menu bar layout. The merging functionality is based on string matching the title of aQMenu entry. These strings are translated (usingQObject::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 theInfo.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 callQApplication::quit()

You can override this behavior by using theQAction::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=newQMenuBar(0);

Note: Donot callQMainWindow::menuBar() to create the shared menu bar, because that menu bar will have theQMainWindow as its parent. That menu bar would only be displayed for the parentQMainWindow.

Note: The text used for the application name in the menu bar is obtained from the value set in theInfo.plist file in the application's bundle. SeeDeploying 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 integratesQMenuBar with the native soft keys. The left soft key can be controlled withQMenuBar::setDefaultAction() and the right soft key can be used to access the menu bar.

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

TheMenus example shows how to useQMenuBar andQMenu. The othermain window application examples also provide menus using these classes.

See alsoQMenu,QShortcut,QAction,Introduction to Apple Human Interface Guidelines,GUI Design Handbook: Menu Bar, andMenus 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 arebelow the document to which they refer.

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

Access functions:

boolisDefaultUp() const
voidsetDefaultUp(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 theQt::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:

boolisNativeMenuBar() const
voidsetNativeMenuBar(bool nativeMenuBar)

See alsovoid-qt-mac-set-native-menubar-bool-enable.

Member Function Documentation

QMenuBar::QMenuBar(QWidget * parent = 0)

Constructs a menu bar with parentparent.

QMenuBar::~QMenuBar()

Destroys the menu bar.

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

Returns theQAction atpt. Returns 0 if there is no action atpt or if the location has a separator.

See alsoaddAction() andaddSeparator().

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

Reimplemented fromQWidget::actionEvent().

QRect QMenuBar::actionGeometry(QAction * act) const

Returns the geometry of actionact as aQRect.

See alsoactionAt().

QAction * QMenuBar::activeAction() const

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

See alsosetActiveAction().

QAction * QMenuBar::addAction(constQString & text)

This is an overloaded function.

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

See alsoQWidget::addAction() andQWidget::actions().

QAction * QMenuBar::addAction(constQString & text, constQObject * receiver, constchar * member)

This is an overloaded function.

This convenience function creates a new action with the giventext. The action'striggered() signal is connected to thereceiver'smember slot. The function adds the newly created action to the menu's list of actions and returns it.

See alsoQWidget::addAction() andQWidget::actions().

void QMenuBar::addAction(QAction * action)

This is an overloaded function.

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

See alsoQMenu::addAction(),QWidget::addAction(), andQWidget::actions().

QAction * QMenuBar::addMenu(QMenu * menu)

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

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

See alsoQWidget::addAction() andQMenu::menuAction().

QMenu * QMenuBar::addMenu(constQString & title)

Appends a newQMenu withtitle to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

See alsoQWidget::addAction() andQMenu::menuAction().

QMenu * QMenuBar::addMenu(constQIcon & icon, constQString & title)

Appends a newQMenu withicon andtitle to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

See alsoQWidget::addAction() andQMenu::menuAction().

QAction * QMenuBar::addSeparator()

Appends a separator to the menu.

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

Reimplemented fromQWidget::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 themenu 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 alsoremoveAction().

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 oncorner.

Note:Using a corner other thanQt::TopRightCorner orQt::TopLeftCorner will result in a warning.

See alsosetCornerWidget().

QAction * QMenuBar::defaultAction() const

Returns the current default action.

This function was introduced in Qt 4.4.

See alsosetDefaultAction().

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

Reimplemented fromQObject::event().

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

Reimplemented fromQObject::eventFilter().

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

Reimplemented fromQWidget::focusInEvent().

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

Reimplemented fromQWidget::focusOutEvent().

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

Reimplemented fromQWidget::heightForWidth().

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

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 alsotriggered() andQAction::hovered().

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

Initializeoption with the values from the menu bar and information fromaction. This method is useful for subclasses when they need aQStyleOptionMenuItem, but don't want to fill in all the information themselves.

See alsoQStyleOption::initFrom() andQMenu::initStyleOption().

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

This convenience function insertsmenu before actionbefore and returns the menus menuAction().

See alsoQWidget::insertAction() andaddMenu().

QAction * QMenuBar::insertSeparator(QAction * before)

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

See alsoQWidget::insertAction() andaddSeparator().

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

Reimplemented fromQWidget::keyPressEvent().

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

Reimplemented fromQWidget::leaveEvent().

[virtual]QSize QMenuBar::minimumSizeHint() const

Reimplemented fromQWidget::minimumSizeHint().

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

Reimplemented fromQWidget::mouseMoveEvent().

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

Reimplemented fromQWidget::mousePressEvent().

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

Reimplemented fromQWidget::mouseReleaseEvent().

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

Reimplemented fromQWidget::paintEvent().

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

Reimplemented fromQWidget::resizeEvent().

void QMenuBar::setActiveAction(QAction * act)

Sets the currently highlighted action toact.

This function was introduced in Qt 4.1.

See alsoactiveAction().

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

This sets the givenwidget to be shown directly on the left of the first menu item, or on the right of the last menu item, depending oncorner.

The menu bar takes ownership ofwidget, reparenting it into the menu bar. However, if thecorner 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 thanQt::TopRightCorner orQt::TopLeftCorner will result in a warning.

See alsocornerWidget().

void QMenuBar::setDefaultAction(QAction * act)

Sets the default action toact.

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

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

Reimplemented fromQWidget::setVisible().

[virtual]QSize QMenuBar::sizeHint() const

Reimplemented fromQWidget::sizeHint().

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

Reimplemented fromQObject::timerEvent().

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

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.

Note:QMenuBar has to have ownership of theQMenu in order this signal to work.

Normally, you connect each menu action to a single slot usingQAction::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 alsohovered() andQAction::triggered().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp