QMenu 類提供用於菜單欄、上下文菜單及其它彈齣菜單的菜單 Widget。 更多...
| 頭: | #include <QMenu> |
| qmake: | QT += widgets |
| 繼承: | QWidget |
|
|
| QMenu (const QString & title , QWidget * parent = nullptr) | |
| QMenu (QWidget * parent = nullptr) | |
| virtual | ~QMenu () |
| QAction * | actionAt (const QPoint & pt ) const |
| QRect | actionGeometry (QAction * act ) const |
| QAction * | activeAction () const |
| QAction * | addAction (const QString & text ) |
| QAction * | addAction (const QIcon & icon , const QString & text ) |
| QAction * | addAction (const QString & text , const QObject * receiver , const char * member , const QKeySequence & shortcut = 0) |
| QAction * | addAction (const QIcon & icon , const QString & text , const QObject * receiver , const char * member , const QKeySequence & shortcut = 0) |
| QAction * | addAction (const QString & text , Functor functor , const QKeySequence & shortcut = 0) |
| QAction * | addAction (const QString & text , const QObject * context , Functor functor , const QKeySequence & shortcut = 0) |
| QAction * | addAction (const QIcon & icon , const QString & text , Functor functor , const QKeySequence & shortcut = 0) |
| QAction * | addAction (const QIcon & icon , const QString & text , const QObject * context , Functor functor , const QKeySequence & shortcut = 0) |
| QAction * | addMenu (QMenu * menu ) |
| QMenu * | addMenu (const QString & title ) |
| QMenu * | addMenu (const QIcon & icon , const QString & title ) |
| QAction * | addSection (const QString & text ) |
| QAction * | addSection (const QIcon & icon , const QString & text ) |
| QAction * | addSeparator () |
| void | clear () |
| QAction * | defaultAction () const |
| QAction * | exec () |
| QAction * | exec (const QPoint & p , QAction * action = nullptr) |
| void | hideTearOffMenu () |
| QIcon | icon () const |
| QAction * | insertMenu (QAction * before , QMenu * menu ) |
| QAction * | insertSection (QAction * before , const QString & text ) |
| QAction * | insertSection (QAction * before , const QIcon & icon , const QString & text ) |
| QAction * | insertSeparator (QAction * before ) |
| bool | isEmpty () const |
| bool | isTearOffEnabled () const |
| bool | isTearOffMenuVisible () const |
| QAction * | menuAction () const |
| void | popup (const QPoint & p , QAction * atAction = nullptr) |
| bool | separatorsCollapsible () const |
| void | setActiveAction (QAction * act ) |
| void | setAsDockMenu () |
| void | setDefaultAction (QAction * act ) |
| void | setIcon (const QIcon & icon ) |
| void | setSeparatorsCollapsible (bool collapse ) |
| void | setTearOffEnabled ( bool ) |
| void | setTitle (const QString & title ) |
| void | setToolTipsVisible (bool visible ) |
| void | showTearOffMenu (const QPoint & pos ) |
| void | showTearOffMenu () |
| QString | title () const |
| NSMenu * | toNSMenu () |
| bool | toolTipsVisible () const |
| virtual QSize | sizeHint () const override |
| void | aboutToHide () |
| void | aboutToShow () |
| void | hovered (QAction * action ) |
| void | triggered (QAction * action ) |
| QAction * | exec (QList<QAction *> actions , const QPoint & pos , QAction * at = nullptr, QWidget * parent = nullptr) |
| int | columnCount () const |
| void | initStyleOption (QStyleOptionMenuItem * option , const QAction * action ) const |
| virtual void | actionEvent (QActionEvent * e ) override |
| virtual void | changeEvent (QEvent * e ) override |
| virtual void | enterEvent ( QEvent * ) override |
| virtual bool | event (QEvent * e ) override |
| virtual bool | focusNextPrevChild (bool next ) override |
| virtual void | hideEvent ( QHideEvent * ) 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 | timerEvent (QTimerEvent * e ) override |
| virtual void | wheelEvent (QWheelEvent * e ) override |
菜單 Widget 是選定菜單。它可以要麼是菜單欄中的下拉菜單,要麼是獨立上下文菜單。下拉菜單的展示是通過菜單欄,當用戶點擊各項 (或按下指定快捷鍵) 時。使用 QMenuBar::addMenu () 以將菜單插入菜單欄。上下文菜單通常通過一些特殊鍵盤鍵 (或右鍵點擊) 援引。可以異步執行它們采用 popup () 或同步采用 exec ()。菜單還可以被援引,為響應按鈕按下;這些就像上下文菜單,除如何援引它們外。
菜單由動作項列錶組成。動作的添加是采用 addAction (), addActions () 和 insertAction () 函數。動作垂直錶示且渲染是通過 QStyle 。此外,動作可以擁有文本標簽、在很左側繪製的可選圖標、及快捷鍵序列 (譬如 Ctrl+X)。
可以找到由菜單保持的現有動作采用 actions ().
有 4 種動作項:分隔符、展示子菜單的動作、Widget 和履行動作的動作。分隔符的插入是采用 addSeparator (),子菜單采用 addMenu (),和所有其它項被認為是動作項。
當插入動作項時,通常會指定接收者和槽。會通知接收者,每當項被 triggered() 。此外,QMenu 提供瞭 2 信號 triggered () 和 hovered (),信號 QAction 被觸發從菜單。
清零菜單采用 clear () 和移除單個動作項采用 removeAction ().
此外,QMenu 還可以提供撕離菜單。撕離菜單是包含菜單副本的頂層窗口。這使用戶撕離常用菜單並將它們放在屏幕方便位置處,成為可能。若想要特定菜單具有此功能,插入撕離手柄采用 setTearOffEnabled ()。當使用撕離菜單時,請記住該概念通常不會用於 Microsoft Windows,因此某些用戶可能對它不熟悉。考慮使用 QToolBar 代替。
可以將 Widget 插入菜單采用 QWidgetAction 類。此類的實例用於保持 Widget,並被插入菜單采用 addAction () 重載接受 QAction 。若 QWidgetAction 激發 triggered () 信號,菜單將關閉。
警告: 要使 QMenu 在屏幕上可見, exec () 或 popup () 應該被使用而非 show ().
QMenu 隻可以插入菜單/菜單欄一次。後續插入不起作用 (或將導緻禁用菜單項)。
見 菜單 範例,瞭解如何使用 QMenuBar 和 QMenu 在應用程序中。
重要繼承函數: addAction (), removeAction (), clear (), addSeparator (),和 addMenu ().
另請參閱 QMenuBar , GUI 設計手冊:菜單、下拉及彈齣 , 應用程序範例 ,和 菜單範例 .
此特性保持菜單的圖標
這相當於 QAction::icon 特性為 menuAction ().
默認情況下,若未明確設置圖標,此特性包含 null 圖標。
訪問函數:
| QIcon | icon () const |
| void | setIcon (const QIcon & icon ) |
此特性保持連續分隔符是否應該被摺疊
This property specifies whether consecutive separators in the menu should be visually collapsed to a single one. Separators at the beginning or the end of the menu are also hidden.
默認情況下,此特性為
true
.
該特性在 Qt 4.2 引入。
訪問函數:
| bool | separatorsCollapsible () const |
| void | setSeparatorsCollapsible (bool collapse ) |
此特性保持菜單是否支持被撕離
當為 true 時,菜單包含特殊可撕離項 (經常在菜單頂部展示成虛綫),當觸發它時會創建菜單副本。
此撕離副本活在單獨窗口中。它包含如原始菜單的相同菜單項,除具有可撕離手柄外。
默認情況下,此特性為
false
.
訪問函數:
| bool | isTearOffEnabled () const |
| void | setTearOffEnabled ( bool ) |
此特性保持菜單的標題
這相當於 QAction::text 特性為 menuAction ().
默認情況下,此特性包含空字符串。
訪問函數:
| QString | title () const |
| void | setTitle (const QString & title ) |
此特性保持菜單動作的工具提示是否應可見
此特性指定動作菜單條目,是否展示其工具提示。
默認情況下,此特性為
false
.
該特性在 Qt 5.1 引入。
訪問函數:
| bool | toolTipsVisible () const |
| void | setToolTipsVisible (bool visible ) |
構造菜單采用 title 和 parent .
盡管彈齣菜單始終是頂層 Widget,若傳遞父級將刪除彈齣菜單,當銷毀父級時 (如采用任何其它 QObject ).
另請參閱 title .
構造菜單采用父級 parent .
盡管彈齣菜單始終是頂層 Widget,若傳遞父級將刪除彈齣菜單,當銷毀父級時 (如采用任何其它 QObject ).
[signal]
void
QMenu::
aboutToHide
()
此信號被發射僅僅在菜單對用戶隱藏之前。
該函數在 Qt 4.2 引入。
另請參閱 aboutToShow () 和 hide ().
[signal]
void
QMenu::
aboutToShow
()
此信號被發射僅僅在菜單對用戶展示之前。
另請參閱 aboutToHide () 和 show ().
[signal]
void
QMenu::
hovered
(
QAction
*
action
)
此信號發射,當突顯菜單動作時; action 是導緻信號被發射的動作。
這經常被用於更新狀態信息。
另請參閱 triggered () 和 QAction::hovered ().
[signal]
void
QMenu::
triggered
(
QAction
*
action
)
此信號被發射當觸發此菜單中的動作時。
action 是導緻信號被發射的動作。
通常,連接各菜單動作的 triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as "left justify", "center", "right justify".
注意: This signal is emitted for the main parent menu in a hierarchy. Hence, only the parent menu needs to be connected to a slot; sub-menus need not be connected.
另請參閱 hovered () 和 QAction::triggered ().
[虛擬]
QMenu::
~QMenu
()
銷毀菜單。
返迴項在
pt
;返迴
nullptr
若那裏沒有項。
[override virtual protected]
void
QMenu::
actionEvent
(
QActionEvent
*
e
)
重實現: QWidget::actionEvent (QActionEvent *event).
返迴幾何體對於動作 act .
返迴目前被高亮的動作,或
nullptr
若目前沒有被高亮的動作。
另請參閱 setActiveAction ().
此方便函數創建新動作,采用 text 。函數將新近創建的動作添加到菜單的動作列錶,並返迴它。
另請參閱 QWidget::addAction ().
這是重載函數。
此方便函數創建新動作采用 icon 和一些 text 。函數將新近創建的動作添加到菜單的動作列錶,並返迴它。
另請參閱 QWidget::addAction ().
這是重載函數。
此方便函數創建新動作采用文本 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 receiver 's member slot. The function adds the newly created action to the menu's list of actions and returns it.
另請參閱 QWidget::addAction ().
這是重載函數。
此方便函數創建新動作采用 icon 和一些 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 member slot of the receiver object. The function adds the newly created action to the menu's list of actions, and returns it.
另請參閱 QWidget::addAction ().
這是重載函數。
此方便函數創建新動作采用文本 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 functor . The function adds the newly created action to the menu's list of actions and returns it.
該函數在 Qt 5.6 引入。
這是重載函數。
此方便函數創建新動作采用文本 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 functor . The functor can be a pointer to a member function of the context object. The newly created action is added to the menu's list of actions and a pointer to it is returned.
若 context 對象被銷毀,函子不會被調用。
該函數在 Qt 5.6 引入。
這是重載函數。
此方便函數創建新動作采用 icon 和一些 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 functor . The function adds the newly created action to the menu's list of actions and returns it.
該函數在 Qt 5.6 引入。
這是重載函數。
此方便函數創建新動作采用 icon 和一些 text 和可選快捷方式 shortcut 。動作的 triggered() 信號被連接到 functor 。 functor can be a pointer to a member function of the context object. The newly created action is added to the menu's list of actions and a pointer to it is returned.
若 context 被銷毀,函子不會被調用。
該函數在 Qt 5.6 引入。
此方便函數添加 menu 作為子菜單到此菜單。它返迴 menu 's menuAction ()。此菜單沒有所有權對於 menu .
另請參閱 QWidget::addAction () 和 QMenu::menuAction ().
追加新的 QMenu with title 到菜單。菜單擁有菜單的所有權。返迴新菜單。
另請參閱 QWidget::addAction () 和 QMenu::menuAction ().
追加新的 QMenu with icon and title 到菜單。菜單擁有菜單的所有權。返迴新菜單。
另請參閱 QWidget::addAction () 和 QMenu::menuAction ().
此方便函數創建新區間動作,即:動作采用 QAction::isSeparator () 返迴 true,但還有 text 提示,並將新動作添加到此菜單的動作列錶。它返迴新近創建動作。
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
該函數在 Qt 5.1 引入。
另請參閱 QWidget::addAction ().
此方便函數創建新區間動作,即:動作采用 QAction::isSeparator () 返迴 true,但還有 text and icon 提示,並將新動作添加到此菜單的動作列錶。它返迴新近創建動作。
提示的渲染從屬樣式和平颱。Widget 樣式可以使用文本和圖標信息在針對區間的渲染中,或者可以選擇忽略它們,並渲染區間像簡單分隔符。
該函數在 Qt 5.1 引入。
另請參閱 QWidget::addAction ().
此方便函數創建新分隔符動作,即:動作采用 QAction::isSeparator () 返迴 true,並將新動作添加到此菜單的動作列錶。它返迴新近創建動作。
另請參閱 QWidget::addAction ().
[override virtual protected]
void
QMenu::
changeEvent
(
QEvent
*
e
)
重實現: QWidget::changeEvent (QEvent *event).
刪除菜單的所有動作。由菜單擁有且未在任何其它 Widget 中展示的操作將被刪除。
另請參閱 removeAction ().
[protected]
int
QMenu::
columnCount
() const
If a menu does not fit on the screen it lays itself out so that it does fit. It is style dependent what layout means (for example, on Windows it will use multiple columns).
此函數返迴所需的列數。
返迴當前默認操作。
另請參閱 setDefaultAction ().
[override virtual protected]
void
QMenu::
enterEvent
(
QEvent *
)
重實現: QWidget::enterEvent (QEvent *event).
[override virtual protected]
bool
QMenu::
event
(
QEvent
*
e
)
重實現: QWidget::event (QEvent *event).
同步執行此菜單。
這相當於
exec(pos())
.
這返迴被觸發
QAction
在彈齣菜單 (或其某一子菜單),或
nullptr
若沒有項被觸發 (通常因為用戶按下 Esc 鍵)。
在大多數狀況下,想要自己指定位置,例如,當前鼠標位置:
exec(QCursor::pos());
或對齊 Widget:
exec(somewidget.mapToGlobal(QPoint(0,0)));
或反應 QMouseEvent *e:
exec(e->globalPos());
這是重載函數。
同步執行此菜單。
彈齣菜單以便動作 action 將在指定 global 位置 p 。要將 Widget 局部坐標翻譯成全局坐標,使用 QWidget::mapToGlobal ().
這返迴被觸發
QAction
在彈齣菜單 (或其某一子菜單),或
nullptr
若沒有項被觸發 (通常因為用戶按下 Esc 鍵)。
注意,通常會發射所有信號。若連接 QAction to a slot and call the menu's exec(), you get the result both via the signal-slot connection and in the return value of exec().
常見用法是把菜單定位在當前鼠標位置:
exec(QCursor::pos());
或對齊 Widget:
exec(somewidget.mapToGlobal(QPoint(0, 0)));
或反應 QMouseEvent *e:
exec(e->globalPos());
當定位菜單采用 exec() 或 popup (), bear in mind that you cannot rely on the menu's current size (). For performance reasons, the menu adapts its size only when necessary. So in many cases, the size before and after the show is different. Instead, use sizeHint () which calculates the proper size depending on the menu's current contents.
另請參閱 popup () 和 QWidget::mapToGlobal ().
[static]
QAction
*QMenu::
exec
(
QList
<
QAction
*>
actions
, const
QPoint
&
pos
,
QAction
*
at
= nullptr,
QWidget
*
parent
= nullptr)
這是重載函數。
同步執行菜單。
菜單動作的指定是通過列錶 actions 。菜單將彈齣以便指定動作 at ,齣現在全局位置 pos 。若 at 未指定,則菜單齣現在位置 pos . parent 是菜單的父級 Widget;指定父級將提供上下文當 pos 單憑這不足以決定菜單應該放在哪裏 (如:具有多個桌麵或父級被嵌入 QGraphicsView ).
函數返迴被觸發
QAction
在彈齣菜單 (或其某一子菜單),或
nullptr
若沒有項被觸發 (通常因為用戶按下 Esc 鍵)。
這相當於:
QMenu menu; QAction *at = actions[0]; // Assumes actions is not empty for (QAction *a : qAsConst(actions)) menu.addAction(a); menu.exec(pos, at);
另請參閱 popup () 和 QWidget::mapToGlobal ().
[override virtual protected]
bool
QMenu::
focusNextPrevChild
(
bool
next
)
重實現: QWidget::focusNextPrevChild (bool next).
[override virtual protected]
void
QMenu::
hideEvent
(
QHideEvent *
)
重實現: QWidget::hideEvent (QHideEvent *event).
此函數將強製隱藏撕離菜單,使之從用戶桌麵消失。
另請參閱 showTearOffMenu (), isTearOffMenuVisible (),和 isTearOffEnabled ().
[protected]
void
QMenu::
initStyleOption
(
QStyleOptionMenuItem
*
option
, const
QAction
*
action
) const
初始化 option 采用來自此菜單的值和信息來自 action 。此方法對子類是有用的,當需要 QStyleOptionMenuItem ,但不希望自己填充所有信息。
另請參閱 QStyleOption::initFrom () 和 QMenuBar::initStyleOption ().
此方便函數插入 menu 先於動作 before 並返迴菜單 menuAction ().
另請參閱 QWidget::insertAction () 和 addMenu ().
此方便函數創建新標題動作,即:動作采用 QAction::isSeparator () 返迴 true,但還有 text hint. The function inserts the newly created action into this menu's list of actions before action before 並返迴它。
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
該函數在 Qt 5.1 引入。
另請參閱 QWidget::insertAction () 和 addSection ().
此方便函數創建新標題動作,即:動作采用 QAction::isSeparator () 返迴 true,但還有 text and icon hints. The function inserts the newly created action into this menu's list of actions before action before 並返迴它。
提示的渲染從屬樣式和平颱。Widget 樣式可以使用文本和圖標信息在針對區間的渲染中,或者可以選擇忽略它們,並渲染區間像簡單分隔符。
該函數在 Qt 5.1 引入。
另請參閱 QWidget::insertAction () 和 addSection ().
此方便函數創建新分隔符動作,即:動作采用 QAction::isSeparator () returning true. The function inserts the newly created action into this menu's list of actions before action before 並返迴它。
另請參閱 QWidget::insertAction () 和 addSeparator ().
返迴
true
若不存在插入菜單的可見動作,否則 false。
該函數在 Qt 4.2 引入。
另請參閱 QWidget::actions ().
當菜單被撕離時,第 2 展示菜單會在新窗口中顯示菜單內容。當菜單處於此模式下且菜單可見時,返迴
true
;否則 false。
另請參閱 showTearOffMenu (), hideTearOffMenu (),和 isTearOffEnabled ().
[override virtual protected]
void
QMenu::
keyPressEvent
(
QKeyEvent
*
e
)
重實現: QWidget::keyPressEvent (QKeyEvent *event).
[override virtual protected]
void
QMenu::
leaveEvent
(
QEvent *
)
重實現: QWidget::leaveEvent (QEvent *event).
返迴與此菜單關聯的動作。
[override virtual protected]
void
QMenu::
mouseMoveEvent
(
QMouseEvent
*
e
)
重實現: QWidget::mouseMoveEvent (QMouseEvent *event).
[override virtual protected]
void
QMenu::
mousePressEvent
(
QMouseEvent
*
e
)
重實現: QWidget::mousePressEvent (QMouseEvent *event).
[override virtual protected]
void
QMenu::
mouseReleaseEvent
(
QMouseEvent
*
e
)
重實現: QWidget::mouseReleaseEvent (QMouseEvent *event).
[override virtual protected]
void
QMenu::
paintEvent
(
QPaintEvent
*
e
)
重實現: QWidget::paintEvent (QPaintEvent *event).
顯示菜單以便動作 atAction 將在指定 global 位置 p 。要將 Widget 局部坐標翻譯成全局坐標,使用 QWidget::mapToGlobal ().
當定位菜單采用 exec () or popup(), bear in mind that you cannot rely on the menu's current size (). For performance reasons, the menu adapts its size only when necessary, so in many cases, the size before and after the show is different. Instead, use sizeHint () which calculates the proper size depending on the menu's current contents.
另請參閱 QWidget::mapToGlobal () 和 exec ().
把目前突顯動作設為 act .
另請參閱 activeAction ().
Set this menu to be the dock menu available by option-clicking on the application dock icon. Available on macOS only.
該函數在 Qt 5.2 引入。
This sets the default action to act . The default action may have a visual cue, depending on the current QStyle . A default action usually indicates what will happen by default when a drop occurs.
另請參閱 defaultAction ().
此函數將強製展示撕離菜單,使之齣現在用戶桌麵指定 global 位置 pos .
該函數在 Qt 5.7 引入。
另請參閱 hideTearOffMenu (), isTearOffMenuVisible (),和 isTearOffEnabled ().
這是重載函數。
此函數將強製展示撕離菜單,使之齣現在用戶桌麵鼠標光標下。
該函數在 Qt 5.7 引入。
另請參閱 hideTearOffMenu (), isTearOffMenuVisible (),和 isTearOffEnabled ().
[override virtual]
QSize
QMenu::
sizeHint
() const
重實現訪問函數為特性: QWidget::sizeHint .
[override virtual protected]
void
QMenu::
timerEvent
(
QTimerEvent
*
e
)
重實現: QObject::timerEvent (QTimerEvent *event).
返迴此菜單的本機 NSMenu。隻可用於 macOS。
注意: Qt sets the delegate on the native menu. If you need to set your own delegate, make sure you save the original one and forward any calls to it.
該函數在 Qt 5.2 引入。
[override virtual protected]
void
QMenu::
wheelEvent
(
QWheelEvent
*
e
)
重實現: QWidget::wheelEvent (QWheelEvent *event).