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:

字符串匹配 置換 注意事項
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 () 來創建共享菜單欄,因為該菜單欄將擁有 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.

默認遵循是否 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 並返迴它。

另請參閱 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 ().