QMenuBar 类

The QMenuBar class provides a horizontal menu bar. 更多...

头: #include <QMenuBar>
继承: QWidget

特性

公共函数

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 )

重实现公共函数

virtual int heightForWidth (int) const
virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const

公共槽

virtual void setVisible (bool visible )

信号

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

保护函数

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

重实现保护函数

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 )

额外继承成员

详细描述

The QMenuBar class provides a horizontal menu bar.

菜单栏由下拉菜单项列表组成。添加菜单项采用 addMenu ()。例如,假定 menubar 是指针指向 QMenuBar and fileMenu 是指针指向 QMenu ,以下语句将菜单插入菜单栏:

menubar->addMenu(fileMenu);
					

菜单项文本中的 & 和号,把 Alt+F 设为此菜单的快捷方式 (在菜单栏中,可以使用 && 以获得真正 & 和号)。

不需要布置菜单栏。它将自己的几何体自动设置到父级 Widget 的顶部,并适当改变它每当父级重置大小时。

用法

在大多数主窗口样式应用程序,将使用 menuBar() function provided in QMainWindow ,添加 QMenu 到菜单栏和添加 QAction 到弹出菜单。

范例 (来自 菜单 范例):

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

可以移除菜单项采用 removeAction ().

可以把 Widget 添加到菜单,通过使用实例化的 QWidgetAction 班来保持它们。然后,可以把这些动作以通常方式插入菜单;见 QMenu 文档编制了解更多细节。

从属平台的外观和感觉

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 帮助 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 帮助 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:

字符串匹配 Placement 注意事项
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 preferences Application Menu | Preferences If this entry is not found the Settings item will be disabled
quit or exit Application 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);
					

注意: 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 .

注意: 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 了解更多信息。

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.

范例

The 菜单 范例展示如何使用 QMenuBar and QMenu 。其它 主窗口应用程序范例 还提供使用这些类的菜单。

另请参阅 QMenu , QShortcut , QAction , Apple 人机界面指南介绍 , GUI 设计手册:菜单栏 ,和 菜单范例 .

特性文档编制

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.

访问函数:

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.

该特性在 Qt 4.6 引入。

访问函数:

bool isNativeMenuBar () const
void setNativeMenuBar (bool nativeMenuBar )

另请参阅 void-qt-mac-set-native-menubar-bool-enable .

成员函数文档编制

QMenuBar:: QMenuBar ( QWidget * parent = 0)

构造菜单栏采用父级 parent .

QMenuBar:: ~QMenuBar ()

销毁菜单栏。

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

返回 QAction at pt . Returns 0 if there is no action at pt 或者若位置拥有分隔符。

另请参阅 addAction () 和 addSeparator ().

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

重实现自 QWidget::actionEvent ().

QRect QMenuBar:: actionGeometry ( QAction * act ) const

返回几何体对于动作 act 作为 QRect .

另请参阅 actionAt ().

QAction * QMenuBar:: activeAction () const

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

另请参阅 setActiveAction ().

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

这是重载函数。

此方便函数创建新动作,采用 text 。函数将新近创建的动作添加到菜单的动作列表,并返回它。

另请参阅 QWidget::addAction () 和 QWidget::actions ().

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

这是重载函数。

此方便函数创建新动作采用给定 text 。动作的 triggered () 信号被连接到 receiver 's member slot. The function adds the newly created action to the menu's list of actions and returns it.

另请参阅 QWidget::addAction () 和 QWidget::actions ().

void QMenuBar:: addAction ( QAction * action )

这是重载函数。

追加动作 action to the menu bar's list of actions.

另请参阅 QMenu::addAction (), QWidget::addAction (),和 QWidget::actions ().

QAction * QMenuBar:: addMenu ( QMenu * menu )

追加 menu to the menu bar. Returns the menu's menuAction().

注意: 返回的 QAction 对象可以用于隐藏相应菜单。

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

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

追加新的 QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

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

追加新的 QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

另请参阅 QWidget::addAction () 和 QMenu::menuAction ().

QAction * QMenuBar:: addSeparator ()

将分隔符追加到菜单。

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

重实现自 QWidget::changeEvent ().

void QMenuBar:: clear ()

从菜单栏移除所有动作。

注意: 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.

另请参阅 removeAction ().

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

返回第一菜单项左侧或最后菜单项右侧 Widget,从属 corner .

注意: 使用角落除了 Qt::TopRightCorner or Qt::TopLeftCorner 将导致警告。

另请参阅 setCornerWidget ().

QAction * QMenuBar:: defaultAction () const

返回当前默认操作。

该函数在 Qt 4.4 引入。

另请参阅 setDefaultAction ().

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

重实现自 QObject::event ().

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

重实现自 QObject::eventFilter ().

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

重实现自 QWidget::focusInEvent ().

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

重实现自 QWidget::focusOutEvent ().

[虚拟] int QMenuBar:: heightForWidth ( int ) const

重实现自 QWidget::heightForWidth ().

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

此信号发射,当突显菜单动作时; action 是导致事件发送的动作。

这经常被用于更新状态信息。

另请参阅 triggered () 和 QAction::hovered ().

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

初始化 option 采用的值来自菜单栏和信息来自 action 。此方法对子类是有用的,当需要 QStyleOptionMenuItem ,但不希望自己填充所有信息。

另请参阅 QStyleOption::initFrom () 和 QMenu::initStyleOption ().

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

此方便函数插入 menu 先于动作 before 并返回菜单 menuAction()。

另请参阅 QWidget::insertAction () 和 addMenu ().

QAction * QMenuBar:: insertSeparator ( QAction * before )

此方便函数创建新分隔符动作,即:动作具有 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.

另请参阅 QWidget::insertAction () 和 addSeparator ().

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

重实现自 QWidget::keyPressEvent ().

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

重实现自 QWidget::leaveEvent ().

[虚拟] QSize QMenuBar:: minimumSizeHint () const

重实现自 QWidget::minimumSizeHint ().

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

重实现自 QWidget::mouseMoveEvent ().

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

重实现自 QWidget::mousePressEvent ().

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

重实现自 QWidget::mouseReleaseEvent ().

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

重实现自 QWidget::paintEvent ().

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

重实现自 QWidget::resizeEvent ().

void QMenuBar:: setActiveAction ( QAction * act )

把目前突显动作设为 act .

该函数在 Qt 4.1 引入。

另请参阅 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.

注意: 使用角落除了 Qt::TopRightCorner or Qt::TopLeftCorner 将导致警告。

另请参阅 cornerWidget ().

void QMenuBar:: setDefaultAction ( QAction * act )

将默认动作设为 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.

该函数在 Qt 4.4 引入。

另请参阅 defaultAction ().

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

重实现自 QWidget::setVisible ().

[虚拟] QSize QMenuBar:: sizeHint () const

重实现自 QWidget::sizeHint ().

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

重实现自 QObject::timerEvent ().

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

此信号被发射当属于此菜单栏的菜单动作被触发作为鼠标点击结果时; action 是导致信号被发射的动作。

注意: QMenuBar 必须拥有所有权对于 QMenu 为使此信号工作。

通常,连接每个菜单动作到单个槽使用 QAction::triggered (),但有时想要将几项连接到单个槽 (最常见,若用户从数组中选择)。此信号在这种情况下很有用。

另请参阅 hovered () 和 QAction::triggered ().