QMenuBar Class Reference

[ QtGui module]

QMenuBar 类提供水平菜单栏。 更多...

继承 QWidget .

方法

Qt Signals


详细描述

QMenuBar 类提供水平菜单栏。

A menu bar consists of a list of pull-down menu items. You add menu items with addMenu (). For example, asuming that menubar is a pointer to a QMenuBar and fileMenu 是指针指向 QMenu , the following statement inserts the menu into the menu bar:

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

菜单项可以被移除采用 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.

A menu bar shown in the Plastique widget style. Plastique widget style , like most other styles, handles the Help menu in the same way as it handles any other menu.
A menu bar shown in the Motif widget style. Motif widget style treats Help menus in a special way, placing them at right-hand end of the menu bar.

QMenuBar on Mac OS X

QMenuBar on Mac OS X 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 Mac OS X also provides a menu bar merging feature to make QMenuBar conform more closely to accepted Mac OS X 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:

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 QApplication.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 () 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 menu bar is obtained from the value set in the Info.plist file in the application's bundle. See Deploying an Application on Mac OS X for more information.

QMenuBar on Windows CE

QMenuBar on Windows CE is a wrapper for using the system-wide menu bar, similar to the Mac. This feature is activated for Windows Mobile and integrates QMenuBar with the native soft keys. The left soft key can be controlled with QMenuBar.setDefaultAction () and the right soft key can be used to access the menu bar.

hovered () signal is not supported for the native menu integration. Also, it is not possible to display an icon in a native menu on Windows Mobile.

范例

菜单 example shows how to use QMenuBar and QMenu 。其它 main window application examples 还提供使用这些类的菜单。


方法文档编制

QMenuBar.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造菜单栏采用父级 parent .

QAction QMenuBar.actionAt ( self , QPoint )

返回 QAction at pt . Returns 0 if there is no action at pt or if the location has a separator.

另请参阅 addAction () 和 addSeparator ().

QMenuBar.actionEvent ( self , QActionEvent )

重实现自 QWidget.actionEvent ().

QRect QMenuBar.actionGeometry ( self , QAction )

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

另请参阅 actionAt ().

QAction QMenuBar.activeAction ( self )

返回 QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.

另请参阅 setActiveAction ().

QMenuBar.addAction ( self , QAction   action )

这是重载函数。

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

另请参阅 QWidget.addAction () 和 QWidget.actions ().

QAction QMenuBar.addAction ( self , QString  text )

这是重载函数。

此方便函数创建新动作采用给定 text 。动作的 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.

另请参阅 QWidget.addAction () 和 QWidget.actions ().

QAction QMenuBar.addAction ( self , QString  text , QObject   receiver , SLOT()SLOT()  member )

这是重载函数。

追加动作 action to the menu bar's list of actions.

另请参阅 QMenu.addAction (), QWidget.addAction (),和 QWidget.actions ().

QAction QMenuBar.addAction ( self , QString  text , callable  receiver )

QAction QMenuBar.addMenu ( self , QMenu   menu )

追加 menu to the menu bar. Returns the menu's menuAction().

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

另请参阅 QWidget.addAction () 和 QMenu.menuAction ().

QMenu QMenuBar.addMenu ( self , QString  title )

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

另请参阅 QWidget.addAction () 和 QMenu.menuAction ().

QMenu QMenuBar.addMenu ( self , QIcon   icon , QString  title )

追加新 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 ().

QAction QMenuBar.addSeparator ( self )

将分隔符追加到菜单。

QMenuBar.changeEvent ( self , QEvent )

重实现自 QWidget.changeEvent ().

QMenuBar.clear ( self )

从菜单栏移除所有动作。

注意: On Mac OS X, 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 在 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 ().

QWidget QMenuBar.cornerWidget ( self , Qt.Corner   corner  = Qt.TopRightCorner)

Returns the widget on the left of the first or on the right of the last menu item, depending on corner .

注意: 使用角落除了 Qt.TopRightCorner or Qt.TopLeftCorner will result in a warning.

另请参阅 setCornerWidget ().

bool QMenuBar.event ( self , QEvent )

重实现自 QObject.event ().

bool QMenuBar.eventFilter ( self , QObject , QEvent )

重实现自 QObject.eventFilter ().

QMenuBar.focusInEvent ( self , QFocusEvent )

重实现自 QWidget.focusInEvent ().

QMenuBar.focusOutEvent ( self , QFocusEvent )

重实现自 QWidget.focusOutEvent ().

int QMenuBar.heightForWidth ( self , int)

重实现自 QWidget.heightForWidth ().

QMenuBar.initStyleOption ( self , QStyleOptionMenuItem   option , QAction   action )

初始化 option with the values from the menu bar and information from action . This method is useful for subclasses when they need a QStyleOptionMenuItem , but don't want to fill in all the information themselves.

另请参阅 QStyleOption.initFrom () 和 QMenu.initStyleOption ().

QAction QMenuBar.insertMenu ( self , QAction   before , QMenu   menu )

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

另请参阅 QWidget.insertAction () 和 addMenu ().

QAction QMenuBar.insertSeparator ( self , QAction   before )

This convenience function creates a new separator action, i.e. an action with QAction.isSeparator () returning true. The function inserts the newly created action into this menu bar's list of actions before action before 并返回 it.

另请参阅 QWidget.insertAction () 和 addSeparator ().

bool QMenuBar.isDefaultUp ( self )

bool QMenuBar.isNativeMenuBar ( self )

QMenuBar.keyPressEvent ( self , QKeyEvent )

重实现自 QWidget.keyPressEvent ().

QMenuBar.leaveEvent ( self , QEvent )

重实现自 QWidget.leaveEvent ().

QSize QMenuBar.minimumSizeHint ( self )

重实现自 QWidget.minimumSizeHint ().

QMenuBar.mouseMoveEvent ( self , QMouseEvent )

重实现自 QWidget.mouseMoveEvent ().

QMenuBar.mousePressEvent ( self , QMouseEvent )

重实现自 QWidget.mousePressEvent ().

QMenuBar.mouseReleaseEvent ( self , QMouseEvent )

重实现自 QWidget.mouseReleaseEvent ().

QMenuBar.paintEvent ( self , QPaintEvent )

重实现自 QWidget.paintEvent ().

QMenuBar.resizeEvent ( self , QResizeEvent )

重实现自 QWidget.resizeEvent ().

QMenuBar.setActiveAction ( self , QAction   action )

把目前突显动作设为 act .

该函数在 Qt 4.1 引入。

另请参阅 activeAction ().

QMenuBar.setCornerWidget ( self , QWidget   widget , Qt.Corner   corner  = Qt.TopRightCorner)

widget argument has it's ownership transferred to Qt.

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 will result in a warning.

另请参阅 cornerWidget ().

QMenuBar.setDefaultUp ( self , bool)

QMenuBar.setNativeMenuBar ( self , bool  nativeMenuBar )

QMenuBar.setVisible ( self , bool  visible )

重实现自 QWidget.setVisible ().

QSize QMenuBar.sizeHint ( self )

重实现自 QWidget.sizeHint ().

QMenuBar.timerEvent ( self , QTimerEvent )

重实现自 QObject.timerEvent ().


Qt Signal Documentation

void hovered (QAction*)

This is the default overload of this signal.

此信号被发射当突显菜单动作时; action 是导致事件被发送的动作。

这经常被用于更新状态信息。

另请参阅 triggered () 和 QAction.hovered ().

void triggered (QAction*)

This is the default overload of this signal.

This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of a mouse click; action is the action that caused the signal to be emitted.

注意: QMenuBar has to have ownership of the QMenu in order this signal to work.

通常,连接每个菜单动作到单个槽使用 QAction.triggered (),但 sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is useful in such cases.

另请参阅 hovered () and QAction.triggered ().