PySide.QtGui.QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
菜单 Widget 是选择菜单。它可以是菜单栏中的下拉菜单,或独立上下文菜单。下拉菜单通过菜单栏展示当用户点击各项或按下指定快捷键时。使用 QMenuBar.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 PySide.QtGui.QMenu.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.
|
|
|
Fig. A menu shown in Plastique widget style , Windows XP widget style ,和 Macintosh widget style .
A menu consists of a list of action items. Actions are added with the PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.addActions() and PySide.QtGui.QWidget.insertAction() functions. An action is represented vertically and rendered by PySide.QtGui.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 PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.addSeparator() , submenus with PySide.QtGui.QMenu.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 PySide.QtGui.QAction.triggered() . In addition, PySide.QtGui.QMenu provides two signals, activated() and highlighted() , which signal the PySide.QtGui.QAction that was triggered from the menu.
清零菜单采用 PySide.QtGui.QMenu.clear() and remove individual action items with PySide.QtGui.QWidget.removeAction() .
A PySide.QtGui.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 PySide.QtGui.QMenu.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 PySide.QtGui.QToolBar 代替。
Widgets can be inserted into menus with the PySide.QtGui.QWidgetAction class. Instances of this class are used to hold widgets, and are inserted into menus with the PySide.QtGui.QMenu.addAction() overload that takes a PySide.QtGui.QAction .
Conversely, actions can be added to widgets with the PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.addActions() and PySide.QtGui.QWidget.insertAction() 函数。
警告
To make PySide.QtGui.QMenu visible on the screen, exec() or PySide.QtGui.QMenu.popup() should be used instead of PySide.QtGui.QWidget.show() .
PySide.QtGui.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 PySide.QtGui.QMenuBar and PySide.QtGui.QMenu 在您的应用程序中。
重要继承函数: PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.removeAction() , PySide.QtGui.QMenu.clear() , PySide.QtGui.QMenu.addSeparator() ,和 PySide.QtGui.QMenu.addMenu() .
另请参阅
PySide.QtGui.QMenuBar GUI 设计手册:菜单、下拉及弹出 应用程序范例 菜单范例 Recent Files Example
| 参数: |
|
|---|
构造菜单采用父级 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 PySide.QtCore.QObject ).
构造菜单采用 title 和 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 PySide.QtCore.QObject ).
| 参数: | arg__1 – PySide.QtCore.QPoint |
|---|---|
| 返回类型: | PySide.QtGui.QAction |
返回项在 pt ; returns 0 if there is no item there.
| 参数: | arg__1 – PySide.QtGui.QAction |
|---|---|
| 返回类型: | PySide.QtCore.QRect |
返回几何体对于动作 act .
| 返回类型: | PySide.QtGui.QAction |
|---|
Returns the currently highlighted action, or 0 if no action is currently highlighted.
| 参数: |
|
|---|---|
| 返回类型: |
这是重载函数。
This convenience function creates a new action with an icon 和一些 text and an optional shortcut shortcut 。动作的 PySide.QtGui.QAction.triggered() signal is connected to the member slot of the receiver object. The function adds the newly created action to the menu's list of actions, and returns it.
另请参阅
| 参数: | text – unicode |
|---|---|
| 返回类型: | PySide.QtGui.QAction |
这是重载函数。
此方便函数创建新动作,采用 text 。函数把新创建动作添加到菜单的动作列表,并返回它。
另请参阅
| 参数: |
|
|---|---|
| 返回类型: |
这是重载函数。
This convenience function creates a new action with an icon 和一些 text 。函数把新创建动作添加到菜单的动作列表,并返回它。
另请参阅
| 参数: |
|
|---|
| 参数: |
|
|---|
| 参数: |
|
|---|---|
| 返回类型: |
这是重载函数。
This convenience function creates a new action with the text text and an optional shortcut shortcut 。动作的 PySide.QtGui.QAction.triggered() signal is connected to the receiver ‘s member slot. The function adds the newly created action to the menu's list of actions and returns it.
另请参阅
| 参数: |
|
|---|---|
| 返回类型: |
追加新 PySide.QtGui.QMenu with icon and title to the menu. The menu takes ownership of the menu. Returns the new menu.
| 参数: | title – unicode |
|---|---|
| 返回类型: | PySide.QtGui.QMenu |
追加新 PySide.QtGui.QMenu with title to the menu. The menu takes ownership of the menu. Returns the new menu.
| 参数: | menu – PySide.QtGui.QMenu |
|---|---|
| 返回类型: | PySide.QtGui.QAction |
This convenience function adds menu as a submenu to this menu. It returns menu ‘s PySide.QtGui.QMenu.menuAction() . This menu does not take ownership of menu .
| 返回类型: | PySide.QtGui.QAction |
|---|
此方便函数创建新分隔符动作,即:动作采用 QAction.isSeparator() returning true, and adds the new action to this menu's list of actions. It returns the newly created action.
另请参阅
Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted.
| 返回类型: | PySide.QtCore.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.
| 返回类型: | PySide.QtGui.QAction |
|---|
返回当前默认操作。
| 参数: |
|
|---|---|
| 返回类型: |
| 参数: |
|
|---|---|
| 返回类型: |
这是重载函数。
同步执行此菜单。
弹出菜单以便动作 action will be at the specified global position p . To translate a widget's local coordinates into global coordinates, use QWidget.mapToGlobal() .
这返回被触发 PySide.QtGui.QAction in either the popup menu or one of its submenus, or 0 if no item was triggered (normally because the user pressed Esc).
Note that all signals are emitted as usual. If you connect a PySide.QtGui.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)))
或反应 PySide.QtGui.QMouseEvent *e:
exec_(e.globalPos())
When positioning a menu with exec() or PySide.QtGui.QMenu.popup() , bear in mind that you cannot rely on the menu's current PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.sizeHint() which calculates the proper size depending on the menu's current contents.
| 参数: |
|
|---|---|
| 返回类型: |
| 返回类型: | PySide.QtGui.QAction |
|---|
同步执行此菜单。
这相当于 exec(pos()) .
这返回被触发 PySide.QtGui.QAction in either the popup menu or one of its submenus, or 0 if no item was triggered (normally because the user pressed Esc).
在大多数情况下,想要自己指定位置,例如,当前鼠标位置:
exec_(QCursor.pos())
或对齐 Widget:
exec_(somewidget.mapToGlobal(QPoint(0,0)))
或反应 PySide.QtGui.QMouseEvent *e:
exec_(e.globalPos())
This function will forcibly hide the torn off menu making it disappear from the users desktop.
| 参数: | action – PySide.QtGui.QAction |
|---|
| 返回类型: | PySide.QtGui.QIcon |
|---|
This property holds The icon of the menu.
这相当于 QAction.icon 特性为 PySide.QtGui.QMenu.menuAction() .
默认情况下,若未明确设置图标,此特性包含 null 图标。
| 参数: |
|
|---|
初始化 option with the values from this menu and information from action 。此方法对子类是有用的,当需要 PySide.QtGui.QStyleOptionMenuItem ,但不希望自己填充所有信息。
| 参数: |
|
|---|---|
| 返回类型: |
此方便函数插入 menu 前于动作 before 并返回菜单 PySide.QtGui.QMenu.menuAction() .
| 参数: | before – PySide.QtGui.QAction |
|---|---|
| 返回类型: | PySide.QtGui.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.
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if there are no visible actions inserted into the menu, false otherwise.
另请参阅
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds whether the menu supports being torn off.
When true, the menu contains a special tear-off item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when it is triggered.
This “torn-off” copy lives in a separate window. It contains the same menu items as the original menu, with the exception of the tear-off handle.
默认情况下,此特性为 false。
| 返回类型: | PySide.QtCore.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.
| 返回类型: | PySide.QtGui.QAction |
|---|
返回与此菜单关联的动作。
| 参数: |
|
|---|
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 QWidget.mapToGlobal() .
When positioning a menu with exec() or PySide.QtGui.QMenu.popup() , bear in mind that you cannot rely on the menu's current PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.sizeHint() which calculates the proper size depending on the menu's current contents.
另请参阅
QWidget.mapToGlobal() exec()
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds whether consecutive separators should be collapsed.
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。
| 参数: | act – PySide.QtGui.QAction |
|---|
把目前突显动作设为 act .
| 参数: | arg__1 – PySide.QtGui.QAction |
|---|
This sets the default action to act . The default action may have a visual cue, depending on the current PySide.QtGui.QStyle . A default action usually indicates what will happen by default when a drop occurs.
| 参数: | icon – PySide.QtGui.QIcon |
|---|
This property holds The icon of the menu.
这相当于 QAction.icon 特性为 PySide.QtGui.QMenu.menuAction() .
默认情况下,若未明确设置图标,此特性包含 null 图标。
| 参数: | collapse – PySide.QtCore.bool |
|---|
This property holds whether consecutive separators should be collapsed.
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。
| 参数: | arg__1 – PySide.QtCore.bool |
|---|
This property holds whether the menu supports being torn off.
When true, the menu contains a special tear-off item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when it is triggered.
This “torn-off” copy lives in a separate window. It contains the same menu items as the original menu, with the exception of the tear-off handle.
默认情况下,此特性为 false。
| 参数: | title – unicode |
|---|
This property holds The title of the menu.
这相当于 QAction.text 特性为 PySide.QtGui.QMenu.menuAction() .
默认情况下,此特性包含空字符串。
| 返回类型: | unicode |
|---|
This property holds The title of the menu.
这相当于 QAction.text 特性为 PySide.QtGui.QMenu.menuAction() .
默认情况下,此特性包含空字符串。
| 参数: | action – PySide.QtGui.QAction |
|---|