The QMenuBar class provides a horizontal menu bar. 更多...
| 頭: | #include <QMenuBar> |
| qmake: | QT += widgets |
| 繼承: | QWidget |
| QMenuBar (QWidget * parent = Q_NULLPTR) | |
| ~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 ) |
| 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 * | 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 | setDefaultUp ( bool ) |
| void | setNativeMenuBar (bool nativeMenuBar ) |
| NSMenu * | toNSMenu () |
| virtual int | heightForWidth ( int ) const override |
| virtual QSize | minimumSizeHint () const override |
| virtual QSize | sizeHint () const override |
| virtual void | setVisible (bool visible ) override |
| void | hovered (QAction * action ) |
| void | triggered (QAction * action ) |
| void | initStyleOption (QStyleOptionMenuItem * option , const QAction * action ) const |
| virtual void | actionEvent (QActionEvent * e ) override |
| virtual void | changeEvent (QEvent * e ) override |
| virtual bool | event (QEvent * e ) override |
| virtual bool | eventFilter (QObject * object , QEvent * event ) override |
| virtual void | focusInEvent ( QFocusEvent * ) override |
| virtual void | focusOutEvent ( QFocusEvent * ) override |
| virtual void | keyPressEvent (QKeyEvent * e ) override |
| virtual void | leaveEvent ( QEvent * ) override |
| virtual void | mouseMoveEvent (QMouseEvent * e ) override |
| virtual void | mousePressEvent (QMouseEvent * e ) override |
| virtual void | mouseReleaseEvent (QMouseEvent * e ) override |
| virtual void | paintEvent (QPaintEvent * e ) override |
| virtual void | resizeEvent ( QResizeEvent * ) override |
| virtual void | timerEvent (QTimerEvent * e ) override |
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.
On macOS and on certain Linux desktop environments such as Ubuntu Unity, QMenuBar 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 macOS also provides a menu bar merging feature to make QMenuBar conform more closely to accepted macOS 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 QCoreApplication::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 macOS menu bar is obtained from the value set in the
Info.plist
file in the application's bundle. See
Qt for macOS - 部署
瞭解更多信息。
注意: On Linux, if the com.canonical.AppMenu.Registrar service is available on the D-Bus session bus, then Qt will communicate with it to install the application's menus into the global menu bar, as described.
The 菜單 範例展示如何使用 QMenuBar and QMenu 。其它 主窗口應用程序範例 還提供使用這些類的菜單。
另請參閱 QMenu , QShortcut , QAction , Apple 人機界麵指南介紹 , GUI 設計手冊:菜單欄 ,和 菜單範例 .
此特性保持彈齣取嚮
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 ) |
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 macOS, and Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity). 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, setting this attribute has no effect, and reading this attribute will always return
false
.
默認遵循是否 Qt::AA_DontUseNativeMenuBar attribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
該特性在 Qt 4.6 引入。
訪問函數:
| bool | isNativeMenuBar () const |
| void | setNativeMenuBar (bool nativeMenuBar ) |
構造菜單欄采用父級 parent .
銷毀菜單欄。
返迴 QAction at pt . Returns 0 if there is no action at pt 或者若位置擁有分隔符。
另請參閱 addAction () 和 addSeparator ().
[override virtual protected]
void
QMenuBar::
actionEvent
(
QActionEvent
*
e
)
重實現自 QWidget::actionEvent ().
返迴幾何體對於動作 act 作為 QRect .
另請參閱 actionAt ().
返迴 QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.
另請參閱 setActiveAction ().
這是重載函數。
此方便函數創建新動作,采用 text 。函數將新近創建的動作添加到菜單的動作列錶,並返迴它。
另請參閱 QWidget::addAction () 和 QWidget::actions ().
這是重載函數。
此方便函數創建新動作采用給定 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 ().
追加 menu to the menu bar. Returns the menu's menuAction().
注意: 返迴的 QAction 對象可以用於隱藏相應菜單。
另請參閱 QWidget::addAction () 和 QMenu::menuAction ().
追加新的 QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
另請參閱 QWidget::addAction () 和 QMenu::menuAction ().
追加新的 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 ().
將分隔符追加到菜單。
[override virtual protected]
void
QMenuBar::
changeEvent
(
QEvent
*
e
)
重實現自 QWidget::changeEvent ().
從菜單欄移除所有動作。
注意: On macOS, 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 ().
返迴第一菜單項左側或最後菜單項右側 Widget,從屬 corner .
注意: 使用角落除瞭 Qt::TopRightCorner or Qt::TopLeftCorner 將導緻警告。
另請參閱 setCornerWidget ().
[override virtual protected]
bool
QMenuBar::
event
(
QEvent
*
e
)
重實現自 QObject::event ().
[override virtual protected]
bool
QMenuBar::
eventFilter
(
QObject
*
object
,
QEvent
*
event
)
重實現自 QObject::eventFilter ().
[override virtual protected]
void
QMenuBar::
focusInEvent
(
QFocusEvent *
)
重實現自 QWidget::focusInEvent ().
[override virtual protected]
void
QMenuBar::
focusOutEvent
(
QFocusEvent *
)
重實現自 QWidget::focusOutEvent ().
[override virtual]
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 ().
此方便函數插入 menu 先於動作 before 並返迴菜單 menuAction()。
另請參閱 QWidget::insertAction () 和 addMenu ().
此方便函數創建新分隔符動作,即:動作采用 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 ().
[override virtual protected]
void
QMenuBar::
keyPressEvent
(
QKeyEvent
*
e
)
重實現自 QWidget::keyPressEvent ().
[override virtual protected]
void
QMenuBar::
leaveEvent
(
QEvent *
)
重實現自 QWidget::leaveEvent ().
[override virtual]
QSize
QMenuBar::
minimumSizeHint
() const
重實現自 QWidget::minimumSizeHint ().
[override virtual protected]
void
QMenuBar::
mouseMoveEvent
(
QMouseEvent
*
e
)
重實現自 QWidget::mouseMoveEvent ().
[override virtual protected]
void
QMenuBar::
mousePressEvent
(
QMouseEvent
*
e
)
重實現自 QWidget::mousePressEvent ().
[override virtual protected]
void
QMenuBar::
mouseReleaseEvent
(
QMouseEvent
*
e
)
重實現自 QWidget::mouseReleaseEvent ().
[override virtual protected]
void
QMenuBar::
paintEvent
(
QPaintEvent
*
e
)
重實現自 QWidget::paintEvent ().
[override virtual protected]
void
QMenuBar::
resizeEvent
(
QResizeEvent *
)
重實現自 QWidget::resizeEvent ().
把目前突顯動作設為 act .
該函數在 Qt 4.1 引入。
另請參閱 activeAction ().
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 ().
[override virtual slot]
void
QMenuBar::
setVisible
(
bool
visible
)
重實現自 QWidget::setVisible ().
[override virtual]
QSize
QMenuBar::
sizeHint
() const
重實現自 QWidget::sizeHint ().
[override virtual protected]
void
QMenuBar::
timerEvent
(
QTimerEvent
*
e
)
重實現自 QObject::timerEvent ().
返迴此菜單欄的本機 NSMenu。隻可用於 macOS。
注意: Qt may set the delegate on the native menu bar. If you need to set your own delegate, make sure you save the original one and forward any calls to it.
該函數在 Qt 5.2 引入。
[signal]
void
QMenuBar::
triggered
(
QAction
*
action
)
此信號被發射當屬於此菜單欄的菜單動作被觸發作為鼠標點擊結果時; action 是導緻信號被發射的動作。
注意: QMenuBar 必須擁有所有權對於 QMenu 為使此信號工作。
通常,連接每個菜單動作到單個槽使用 QAction::triggered (),但有時想要將幾項連接到單個槽 (最常見,若用戶從數組中選擇)。此信號在這種情況下很有用。
另請參閱 hovered () 和 QAction::triggered ().