QMenuBar

QMenuBar class provides a horizontal menu bar. 更多

Inheritance diagram of PySide2.QtWidgets.QMenuBar

概要

函数

信号

详细描述

菜单栏由下拉菜单项列表组成。添加菜单项采用 addMenu() . For example, asuming that menubar 是指针指向 QMenuBar and fileMenu 是指针指向 QMenu ,以下语句将菜单插入菜单栏:

menubar.addMenu(fileMenu)
											

The ampersand in the menu item’s text sets Alt+F as a shortcut for this menu. (You can use “&&” to get a real ampersand in the menu bar.)

There is no need to lay out a menu bar. It automatically sets its own geometry to the top of the parent widget and changes it appropriately whenever the parent is resized.

用法

In most main window style applications you would use the menuBar() function provided in QMainWindow , adding QMenu s to the menu bar and adding QAction s to the pop-up menus.

范例 (来自 菜单 范例):

fileMenu = menuBar().addMenu(tr("&File"))
fileMenu.addAction(Act)
												

菜单项可以被移除采用 removeAction() .

Widgets can be added to menus by using instances of the QWidgetAction class to hold them. These actions can then be inserted into menus in the usual way; see the 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.

QMenuBar 作为全局菜单栏

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 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 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:

String matches

Placement

注意事项

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

You can override this behavior by using the menuRole() 特性。

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:

menuBar = QMenuBar()
												

注意

Do not call menuBar() to create the shared menu bar, because that menu bar will have the 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 - Deployment for more information.

注意

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.

范例

菜单 example shows how to use QMenuBar and QMenu 。其它 主窗口应用程序范例 还提供使用这些类的菜单。

class QMenuBar ( [ parent=None ] )
param parent

QWidget

构造菜单栏采用父级 parent .

PySide2.QtWidgets.QMenuBar. actionAt ( arg__1 )
参数

arg__1 QPoint

返回类型

QAction

返回 QAction at pt 。返回 None 若没有动作在 pt 或者若位置拥有分隔符。

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

arg__1 QAction

返回类型

QRect

返回几何体对于动作 act 作为 QRect .

另请参阅

actionAt()

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

QAction

返回 QAction 目前有突显,若有的话,否则 None .

另请参阅

setActiveAction()

PySide2.QtWidgets.QMenuBar. addAction ( arg__1 , arg__2 )
参数
  • arg__1 – unicode

  • arg__2 PyObject

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

text – unicode

返回类型

QAction

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

PySide2.QtWidgets.QMenuBar. addAction ( text , receiver , member )
参数
  • text – unicode

  • receiver QObject

  • member – str

返回类型

QAction

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

title – unicode

返回类型

QMenu

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

  • title – unicode

返回类型

QMenu

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

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

menu QMenu

返回类型

QAction

追加 menu to the menu bar. Returns the menu’s menuAction(). The menu bar does not take ownership of the menu.

注意

返回的 QAction 对象可以用于隐藏相应菜单。

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

QAction

将分隔符追加到菜单。

PySide2.QtWidgets.QMenuBar. clear ( )

从菜单栏移除所有动作。

注意

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

PySide2.QtWidgets.QMenuBar. cornerWidget ( [ corner=Qt.TopRightCorner ] )
参数

corner Corner

返回类型

QWidget

返回第一菜单项左侧或最后菜单项右侧 Widget,从属 corner .

注意

使用角落除了 TopRightCorner or TopLeftCorner 将导致警告。

另请参阅

setCornerWidget()

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

action QAction

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

初始化 option 采用的值来自菜单栏和信息来自 action 。此方法对子类是有用的,当需要 QStyleOptionMenuItem , but don’t want to fill in all the information themselves.

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

QAction

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

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

before QAction

返回类型

QAction

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

PySide2.QtWidgets.QMenuBar. isDefaultUp ( )
返回类型

bool

PySide2.QtWidgets.QMenuBar. isNativeMenuBar ( )
返回类型

bool

PySide2.QtWidgets.QMenuBar. setActiveAction ( action )
参数

action QAction

把目前突显动作设为 act .

另请参阅

activeAction()

PySide2.QtWidgets.QMenuBar. setCornerWidget ( w [ , 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.

注意

使用角落除了 TopRightCorner or TopLeftCorner 将导致警告。

另请参阅

cornerWidget()

PySide2.QtWidgets.QMenuBar. setDefaultUp ( arg__1 )
参数

arg__1 bool

另请参阅

isDefaultUp()

PySide2.QtWidgets.QMenuBar. setNativeMenuBar ( nativeMenuBar )
参数

nativeMenuBar bool

另请参阅

isNativeMenuBar()

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

action QAction