内容表

上一话题

QMdiSubWindow

下一话题

QMenuBar

QMenu

QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus. 更多

Inheritance diagram of PySide2.QtWidgets.QMenu

概要

函数

信号

静态函数

  • def exec_ (actions, pos[, at=None[, parent=None]])

详细描述

../../_images/fusion-menu.png

菜单 Widget 是选择菜单。它可以是菜单栏中的下拉菜单,或独立上下文菜单。下拉菜单通过菜单栏展示当用户点击各项或按下指定快捷键时。使用 addMenu() to insert a menu into a menu bar. Context menus are usually invoked by some special keyboard key or by right-clicking. They can be executed either asynchronously with popup() or synchronously with exec() . Menus can also be invoked in response to button presses; these are just like context menus except for how they are invoked.

动作

A menu consists of a list of action items. Actions are added with the addAction() , addActions() and insertAction() functions. An action is represented vertically and rendered by QStyle . In addition, actions can have a text label, an optional icon drawn on the very left side, and shortcut key sequence such as “Ctrl+X”.

The existing actions held by a menu can be found with actions() .

There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action. Separators are inserted with addSeparator() , submenus with addMenu() , and all other items are considered action items.

When inserting action items you usually specify a receiver and a slot. The receiver will be notifed whenever the item is triggered() . In addition, QMenu provides two signals, triggered() and hovered() , which signal the QAction that was triggered from the menu.

清零菜单采用 clear() and remove individual action items with removeAction() .

A QMenu can also provide a tear-off menu. A tear-off menu is a top-level window that contains a copy of the menu. This makes it possible for the user to “tear off” frequently used menus and position them in a convenient place on the screen. If you want this functionality for a particular menu, insert a tear-off handle with setTearOffEnabled() . When using tear-off menus, bear in mind that the concept isn’t typically used on Microsoft Windows so some users may not be familiar with it. Consider using a QToolBar 代替。

Widgets can be inserted into menus with the QWidgetAction class. Instances of this class are used to hold widgets, and are inserted into menus with the addAction() overload that takes a QAction 。若 QWidgetAction fires the triggered() signal, the menu will close.

警告

To make QMenu visible on the screen, exec() or popup() should be used instead of show() .

在 macOS 采用 Qt 构建针对 Cocoa 的 QMenu

QMenu can be inserted only once in a menu/menubar. Subsequent insertions will have no effect or will result in a disabled menu item.

菜单 example for an example of how to use QMenuBar and QMenu 在您的应用程序中。

重要继承函数: addAction() , removeAction() , clear() , addSeparator() ,和 addMenu() .

class QMenu ( [ parent=None ] )

QMenu(title[, parent=None])

param parent

QWidget

param title

unicode

构造菜单采用父级 parent .

Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject ).

PySide2.QtWidgets.QMenu. aboutToHide ( )
PySide2.QtWidgets.QMenu. aboutToShow ( )
PySide2.QtWidgets.QMenu. actionAt ( arg__1 )
参数

arg__1 QPoint

返回类型

QAction

返回项在 pt ;返回 None 若那里没有项。

PySide2.QtWidgets.QMenu. actionGeometry ( arg__1 )
参数

arg__1 QAction

返回类型

QRect

返回几何体对于动作 act .

PySide2.QtWidgets.QMenu. activeAction ( )
返回类型

QAction

返回目前突显动作,或 None 若没有动作目前被突显。

另请参阅

setActiveAction()

PySide2.QtWidgets.QMenu. addAction ( text )
参数

text – unicode

返回类型

QAction

此方便函数创建新动作,采用 text . The function adds the newly created action to the menu’s list of actions, and returns it.

QMenu 拥有所有权对于返回的 QAction .

另请参阅

addAction()

PySide2.QtWidgets.QMenu. addAction ( text , receiver , member [ , shortcut=0 ] )
参数
  • text – unicode

  • receiver QObject

  • member – str

  • shortcut QKeySequence

返回类型

QAction

PySide2.QtWidgets.QMenu. addAction ( icon , text , receiver , member [ , shortcut=0 ] )
参数
  • icon QIcon

  • text – unicode

  • receiver QObject

  • member – str

  • shortcut QKeySequence

返回类型

QAction

PySide2.QtWidgets.QMenu. addAction ( icon , text )
参数
  • icon QIcon

  • text – unicode

返回类型

QAction

这是重载函数。

This convenience function creates a new action with an icon 和一些 text . The function adds the newly created action to the menu’s list of actions, and returns it.

QMenu 拥有所有权对于返回的 QAction .

另请参阅

addAction()

PySide2.QtWidgets.QMenu. addAction ( arg__1 , arg__2 [ , arg__3=0 ] )
参数
  • arg__1 – unicode

  • arg__2 PyObject

  • arg__3 QKeySequence

PySide2.QtWidgets.QMenu. addAction ( arg__1 , arg__2 , arg__3 [ , arg__4=0 ] )
参数
  • arg__1 QIcon

  • arg__2 – unicode

  • arg__3 PyObject

  • arg__4 QKeySequence

PySide2.QtWidgets.QMenu. addMenu ( menu )
参数

menu QMenu

返回类型

QAction

This convenience function adds menu as a submenu to this menu. It returns menu ‘s menuAction() . This menu does not take ownership of menu .

PySide2.QtWidgets.QMenu. addMenu ( icon , title )
参数
  • icon QIcon

  • title – unicode

返回类型

QMenu

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

PySide2.QtWidgets.QMenu. addMenu ( title )
参数

title – unicode

返回类型

QMenu

PySide2.QtWidgets.QMenu. addSection ( icon , text )
参数
  • icon QIcon

  • text – unicode

返回类型

QAction

此方便函数创建新的区间动作,即动作采用 isSeparator() returning true but also having text and icon hints, and adds the new action to this menu’s list of actions. It returns the newly created action.

The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.

QMenu 拥有所有权对于返回的 QAction .

另请参阅

addAction()

PySide2.QtWidgets.QMenu. addSection ( text )
参数

text – unicode

返回类型

QAction

此方便函数创建新的区间动作,即动作采用 isSeparator() returning true but also having text hint, and adds the new action to this menu’s list of actions. It returns the newly created action.

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.

QMenu 拥有所有权对于返回的 QAction .

另请参阅

addAction()

PySide2.QtWidgets.QMenu. addSeparator ( )
返回类型

QAction

此方便函数创建新分隔符动作,即:动作采用 isSeparator() returning true, and adds the new action to this menu’s list of actions. It returns the newly created action.

QMenu 拥有所有权对于返回的 QAction .

另请参阅

addAction()

PySide2.QtWidgets.QMenu. clear ( )

Removes all the menu’s actions. Actions owned by the menu and not shown in any other widget are deleted.

另请参阅

removeAction()

PySide2.QtWidgets.QMenu. columnCount ( )
返回类型

int

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

This functions returns the number of columns necessary.

PySide2.QtWidgets.QMenu. defaultAction ( )
返回类型

QAction

返回当前默认操作。

PySide2.QtWidgets.QMenu. exec_ ( pos [ , at=None ] )
参数
返回类型

QAction

这是重载函数。

同步执行此菜单。

弹出菜单以便动作 action will be at the specified global position p . To translate a widget’s local coordinates into global coordinates, use mapToGlobal() .

这返回被触发 QAction 在弹出菜单 (或其某一子菜单),或 None 若没有项被触发 (通常因为用户按下 Esc 键)。

Note that all signals are emitted as usual. If you connect a 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() .

Common usage is to position the menu at the current mouse position:

exec_(QCursor.pos())
												

或对齐 Widget:

exec_(somewidget.mapToGlobal(QPoint(0, 0)))
												

或反应 QMouseEvent *e:

exec_(e.globalPos())
												

When positioning a menu with 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.

static PySide2.QtWidgets.QMenu. exec_ ( actions , pos [ , at=None [ , parent=None ] ] )
参数
返回类型

QAction

这是重载函数。

同步执行菜单。

The menu’s actions are specified by the list of actions . The menu will pop up so that the specified action, at ,出现在全局位置 pos 。若 at 未指定,则菜单出现在位置 pos . parent is the menu’s parent widget; specifying the parent will provide context when pos 单凭这不足以决定菜单应该放在哪里 (如:具有多个桌面或父级被嵌入 QGraphicsView ).

函数返回被触发 QAction 在弹出菜单 (或其某一子菜单),或 None 若没有项被触发 (通常因为用户按下 Esc 键)。

这相当于:

menu = QMenu()
at = actions[0] // Assumes actions is not empty
for a in actions:
    menu.addAction(a)
menu.exec_(pos, at)
												
PySide2.QtWidgets.QMenu. exec_ ( )
返回类型

QAction

同步执行此菜单。

这相当于 exec(pos()) .

这返回被触发 QAction 在弹出菜单 (或其某一子菜单),或 None 若没有项被触发 (通常因为用户按下 Esc 键)。

In most situations you’ll want to specify the position yourself, for example, the current mouse position:

exec_(QCursor.pos())
												

或对齐 Widget:

exec_(somewidget.mapToGlobal(QPoint(0,0)))
												

或反应 QMouseEvent *e:

exec_(e.globalPos())
												
PySide2.QtWidgets.QMenu. hideTearOffMenu ( )

This function will forcibly hide the torn off menu making it disappear from the user’s desktop.

PySide2.QtWidgets.QMenu. hovered ( action )
参数

action QAction

PySide2.QtWidgets.QMenu. icon ( )
返回类型

QIcon

另请参阅

setIcon()

PySide2.QtWidgets.QMenu. initStyleOption ( option , action )
参数

初始化 option with the values from this menu and information from action 。此方法对子类是有用的,当需要 QStyleOptionMenuItem , but don’t want to fill in all the information themselves.

PySide2.QtWidgets.QMenu. insertMenu ( before , menu )
参数
返回类型

QAction

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

PySide2.QtWidgets.QMenu. insertSection ( before , icon , text )
参数
  • before QAction

  • icon QIcon

  • text – unicode

返回类型

QAction

此方便函数创建新标题动作,即:动作采用 isSeparator() returning true but also having text and icon hints. The function inserts the newly created action into this menu’s list of actions before action before and returns it.

The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.

QMenu 拥有所有权对于返回的 QAction .

PySide2.QtWidgets.QMenu. insertSection ( before , text )
参数
返回类型

QAction

此方便函数创建新标题动作,即:动作采用 isSeparator() returning true but also having text hint. The function inserts the newly created action into this menu’s list of actions before action before and returns it.

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.

QMenu 拥有所有权对于返回的 QAction .

PySide2.QtWidgets.QMenu. insertSeparator ( before )
参数

before QAction

返回类型

QAction

此方便函数创建新分隔符动作,即:动作采用 isSeparator() returning true. The function inserts the newly created action into this menu’s list of actions before action before and returns it.

QMenu 拥有所有权对于返回的 QAction .

PySide2.QtWidgets.QMenu. isEmpty ( )
返回类型

bool

返回 true if there are no visible actions inserted into the menu, false otherwise.

另请参阅

actions()

PySide2.QtWidgets.QMenu. isTearOffEnabled ( )
返回类型

bool

PySide2.QtWidgets.QMenu. isTearOffMenuVisible ( )
返回类型

bool

When a menu is torn off a second menu is shown to display the menu contents in a new window. When the menu is in this mode and the menu is visible returns true ; otherwise false.

PySide2.QtWidgets.QMenu. menuAction ( )
返回类型

QAction

返回与此菜单关联的动作。

PySide2.QtWidgets.QMenu. popup ( pos [ , at=None ] )
参数

Displays the menu so that the action atAction will be at the specified global position p . To translate a widget’s local coordinates into global coordinates, use mapToGlobal() .

When positioning a menu with exec() or , 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.

另请参阅

mapToGlobal() exec()

PySide2.QtWidgets.QMenu. separatorsCollapsible ( )
返回类型

bool

PySide2.QtWidgets.QMenu. setActiveAction ( act )
参数

act QAction

把目前突显动作设为 act .

另请参阅

activeAction()

PySide2.QtWidgets.QMenu. setDefaultAction ( arg__1 )
参数

arg__1 QAction

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

PySide2.QtWidgets.QMenu. setIcon ( icon )
参数

icon QIcon

另请参阅

icon()

PySide2.QtWidgets.QMenu. setSeparatorsCollapsible ( collapse )
参数

collapse bool

PySide2.QtWidgets.QMenu. setTearOffEnabled ( arg__1 )
参数

arg__1 bool

PySide2.QtWidgets.QMenu. setTitle ( title )
参数

title – unicode

另请参阅

title()

PySide2.QtWidgets.QMenu. setToolTipsVisible ( visible )
参数

visible bool

另请参阅

toolTipsVisible()

PySide2.QtWidgets.QMenu. showTearOffMenu ( )

这是重载函数。

This function will forcibly show the torn off menu making it appear on the user’s desktop under the mouse currsor.

PySide2.QtWidgets.QMenu. showTearOffMenu ( pos )
参数

pos QPoint

This function will forcibly show the torn off menu making it appear on the user’s desktop at the specified global position pos .

PySide2.QtWidgets.QMenu. title ( )
返回类型

unicode

另请参阅

setTitle()

PySide2.QtWidgets.QMenu. toolTipsVisible ( )
返回类型

bool

PySide2.QtWidgets.QMenu. triggered ( action )
参数

action QAction