QMenuBar Class
头文件: | #include <QMenuBar> |
qmake: | QT += widgets |
基类: | QWidget |
属性
- defaultUp : bool
- nativeMenuBar : bool
公有函数
QMenuBar(QWidget *parent = Q_NULLPTR) | |
~QMenuBar() | |
QAction * | actionAt(const QPoint &pt) const |
QRect | actionGeometry(QAction *act) const |
QAction * | activeAction() const |
QAction * | addAction(const QString &text) |
QAction * | addAction(const QString &text, const QObject *receiver, const char *member) |
QAction * | addMenu(QMenu *menu) |
QMenu * | addMenu(const QString &title) |
QMenu * | addMenu(const QIcon &icon, const QString &title) |
QAction * | addSeparator() |
void | clear() |
QWidget * | cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const |
QAction * | insertMenu(QAction *before, QMenu *menu) |
QAction * | insertSeparator(QAction *before) |
bool | isDefaultUp() const |
bool | isNativeMenuBar() const |
void | setActiveAction(QAction *act) |
void | setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner) |
void | setDefaultUp(bool) |
void | setNativeMenuBar(bool nativeMenuBar) |
NSMenu * | toNSMenu() |
重新实现的公有函数
virtual int | heightForWidth(int) const override |
virtual QSize | minimumSizeHint() const override |
virtual QSize | sizeHint() const override |
- 214 个公有函数继承自 QWidget
- 32 个公有函数继承自 QObject
- 14 个公有函数继承自 QPaintDevice
公有槽函数
virtual void | setVisible(bool visible) override |
信号
受保护的函数
void | initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const |
重新实现的受保护函数
virtual void | actionEvent(QActionEvent *e) override |
virtual void | changeEvent(QEvent *e) override |
virtual bool | event(QEvent *e) override |
virtual bool | eventFilter(QObject *object, QEvent *event) override |
virtual void | focusInEvent(QFocusEvent *) override |
virtual void | focusOutEvent(QFocusEvent *) 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 | resizeEvent(QResizeEvent *) override |
virtual void | timerEvent(QTimerEvent *e) override |
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
其他继承的成员
- 1 个公有变量继承自 QObject
- 5 个静态公有成员继承自 QWidget
- 10 个静态公有成员继承自 QObject
- 1 个受保护的槽函数继承自 QWidget
- 2 个受保护的变量继承自 QObject
- 1 protected type inherited from QPaintDevice
详细描述
QMenuBar 提供一个水平菜单栏.
菜单栏由一组弹出菜单组成. 你可以调用 addMenu() 添加菜单. 例如, 假设 menubar
i是一个 QMenuBar 类型的指针, fileMenu
是一个 QMenu 类型的指针, 下列代码将菜单加入菜单栏:
menubar->addMenu(fileMenu);
菜单('&File')的文本包含"&"符号, 指定菜单的快捷方式是'Alt+F'. (菜单栏使用 "&&" 显示 "&".)
你不需要考虑菜单栏布局, 菜单栏自动将其加入父窗口的顶部, 并跟随父窗口调整大小.
用法
在多数应用程序中, 你可以调用QMainWindow::menuBar() 获取菜单栏, 然后调用addMenu() 将 QMenu加入菜单栏, 最后调用 addAction() 将 QAction 添加到弹出菜单.
示例 (摘自 Menus):
fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct);
调用 removeAction() 移除菜单.
Widget借助 QWidgetAction 将其加入菜单栏. 然后采用通用方式加入action; 详见 QMenu.
平台相关的外观
不同的平台有不同的菜单栏外观和交互方式. 例如, windows系统在按下 Alt 键后, 菜单的快捷键才会显示下划线.
QMenuBar作为全局菜单栏
在 macOS 和某些Linux桌面环境(如Ubuntu Unity)上, QMenuBar 是一个系统级菜单栏的包装器. 在一个对话框中, 如果你有多个菜单栏, 则最外层的菜单栏 (具有 Qt::Window 标识) 作为系统级菜单栏.
Qt为 macOS 提供一个菜单栏合并功能, 使 QMenuBar 更符合 macOS 菜单栏布局. 合并功能基于 QMenu 的标题文本. 这些字符串在 "QMenuBar" 翻译 (使用 QObject::tr()). 如果一个菜单被移动, 槽函数仍然在原来的位置生效. 下表列出查找字符串及匹配项位置:
String matches | Placement | Notes |
---|---|---|
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 QCoreApplication::quit() |
你可以使用 QAction::menuRole() 属性重新定义此行为.
如果你希望Mac应用程序中的所有窗口共享一个菜单栏, 那么你必须创建一个没有父对象的菜单栏. 按以下方式创建无父菜单栏:
QMenuBar *menuBar = new QMenuBar(0);
注意: 不要 调用 QMainWindow::menuBar() 创建共享菜单栏, 因为这类菜单栏将 QMainWindow 作为父对象. 并在 QMainWindow 窗口显示.
注意: 在 macOS 系统中, 应用程序的菜单栏文本在其关联的 Info.plist
获取. 详见 Qt for macOS - Deployment.
注意: 在Linux上, 如果 com.canonical.AppMenu.Registrar 服务在D-Bus会话总线上可用, 那么Qt将与其通信, 以将应用程序的菜单安装到全局菜单栏.
示例
Menus 示例展示如何使用 QMenuBar 和 QMenu. 其他 main window application examples 也提供如何使用菜单的相关类.
参见 QMenu, QShortcut, QAction, Introduction to Apple Human Interface Guidelines, GUI Design Handbook: Menu Bar, Menus Example.
属性
defaultUp : bool
This property holds the popup orientation
The default popup orientation. By default, menus pop "down" the screen. By setting the property to true, the menu will pop "up". You might call this for menus that are below the document to which they refer.
If the menu would not fit on the screen, the other direction is used automatically.
访问函数:
bool | isDefaultUp() const |
void | setDefaultUp(bool) |
nativeMenuBar : bool
This property holds whether or not a menubar will be used as a native menubar on platforms that support it
This property specifies whether or not the menubar should be used as a native menubar on platforms that support it. The currently supported platforms are macOS, and Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity). If this property is true
, the menubar is used in the native menubar and is not in the window of its parent; if false
the menubar remains in the window. On other platforms, setting this attribute has no effect, and reading this attribute will always return false
.
The default is to follow whether the Qt::AA_DontUseNativeMenuBar attribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
This property was introduced in Qt 4.6.
访问函数:
bool | isNativeMenuBar() const |
void | setNativeMenuBar(bool nativeMenuBar) |
成员函数
QMenuBar::QMenuBar(QWidget *parent = Q_NULLPTR)
Constructs a menu bar with parent parent.
QMenuBar::~QMenuBar()
Destroys the menu bar.
QAction *QMenuBar::actionAt(const QPoint &pt) const
Returns the QAction at pt. Returns 0 if there is no action at pt or if the location has a separator.
参见 addAction() and addSeparator().
[override virtual protected]
void QMenuBar::actionEvent(QActionEvent *e)
重新实现 QWidget::actionEvent().
QRect QMenuBar::actionGeometry(QAction *act) const
Returns the geometry of action act as a QRect.
参见 actionAt().
QAction *QMenuBar::activeAction() const
Returns the QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.
参见 setActiveAction().
QAction *QMenuBar::addAction(const QString &text)
This is an overloaded function.
This convenience function creates a new action with text. The function adds the newly created action to the menu's list of actions, and returns it.
参见 QWidget::addAction() and QWidget::actions().
QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char *member)
This is an overloaded function.
This convenience function creates a new action with the given text. The action's 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() and QWidget::actions().
QAction *QMenuBar::addMenu(QMenu *menu)
Appends menu to the menu bar. Returns the menu's menuAction().
Note: The returned QAction object can be used to hide the corresponding menu.
参见 QWidget::addAction() and QMenu::menuAction().
QMenu *QMenuBar::addMenu(const QString &title)
Appends a new QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
参见 QWidget::addAction() and QMenu::menuAction().
QMenu *QMenuBar::addMenu(const QIcon &icon, const QString &title)
Appends a new QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
参见 QWidget::addAction() and QMenu::menuAction().
QAction *QMenuBar::addSeparator()
Appends a separator to the menu.
[override virtual protected]
void QMenuBar::changeEvent(QEvent *e)
重新实现 QWidget::changeEvent().
void QMenuBar::clear()
Removes all the actions from the menu bar.
Note: 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().
QWidget *QMenuBar::cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const
Returns the widget on the left of the first or on the right of the last menu item, depending on corner.
Note: Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.
参见 setCornerWidget().
[override virtual protected]
bool QMenuBar::event(QEvent *e)
重新实现 QObject::event().
[override virtual protected]
bool QMenuBar::eventFilter(QObject *object, QEvent *event)
重新实现 QObject::eventFilter().
[override virtual protected]
void QMenuBar::focusInEvent(QFocusEvent *)
重新实现 QWidget::focusInEvent().
[override virtual protected]
void QMenuBar::focusOutEvent(QFocusEvent *)
重新实现 QWidget::focusOutEvent().
[override virtual]
int QMenuBar::heightForWidth(int) const
重新实现 QWidget::heightForWidth().
[signal]
void QMenuBar::hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the event to be sent.
Often this is used to update status information.
参见 triggered() and QAction::hovered().
[protected]
void QMenuBar::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
Initialize 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() and QMenu::initStyleOption().
QAction *QMenuBar::insertMenu(QAction *before, QMenu *menu)
This convenience function inserts menu before action before and returns the menus menuAction().
参见 QWidget::insertAction() and addMenu().
QAction *QMenuBar::insertSeparator(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 and returns it.
参见 QWidget::insertAction() and addSeparator().
[override virtual protected]
void QMenuBar::keyPressEvent(QKeyEvent *e)
重新实现 QWidget::keyPressEvent().
[override virtual protected]
void QMenuBar::leaveEvent(QEvent *)
重新实现 QWidget::leaveEvent().
[override virtual]
QSize QMenuBar::minimumSizeHint() const
重新实现 QWidget::minimumSizeHint().
[override virtual protected]
void QMenuBar::mouseMoveEvent(QMouseEvent *e)
重新实现 QWidget::mouseMoveEvent().
[override virtual protected]
void QMenuBar::mousePressEvent(QMouseEvent *e)
重新实现 QWidget::mousePressEvent().
[override virtual protected]
void QMenuBar::mouseReleaseEvent(QMouseEvent *e)
重新实现 QWidget::mouseReleaseEvent().
[override virtual protected]
void QMenuBar::paintEvent(QPaintEvent *e)
重新实现 QWidget::paintEvent().
[override virtual protected]
void QMenuBar::resizeEvent(QResizeEvent *)
重新实现 QWidget::resizeEvent().
void QMenuBar::setActiveAction(QAction *act)
Sets the currently highlighted action to act.
This function was introduced in Qt 4.1.
参见 activeAction().
void QMenuBar::setCornerWidget(QWidget *widget, Qt::Corner 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.
Note: Using a corner other than Qt::TopRightCorner or Qt::TopLeftCorner will result in a warning.
参见 cornerWidget().
[override virtual slot]
void QMenuBar::setVisible(bool visible)
重新实现 QWidget::setVisible().
[override virtual]
QSize QMenuBar::sizeHint() const
重新实现 QWidget::sizeHint().
[override virtual protected]
void QMenuBar::timerEvent(QTimerEvent *e)
重新实现 QObject::timerEvent().
NSMenu *QMenuBar::toNSMenu()
Returns the native NSMenu for this menu bar. Available on macOS only.
Note: Qt may set the delegate on the native menu bar. If you need to set your own delegate, make sure you save the original one and forward any calls to it.
This function was introduced in Qt 5.2.
[signal]
void QMenuBar::triggered(QAction *action)
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.
Note: QMenuBar has to have ownership of the QMenu in order this signal to work.
Normally, you connect each menu action to a single slot using QAction::triggered(), but 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().