(1)
(2)
(3)
++
(4)
(5)
(6)
(7)以下源代码来自于头文件 qmenubar . h :
#ifndef QMENUBAR_H
#define QMENUBAR_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qmenu.h>
QT_REQUIRE_CONFIG(menubar);
QT_BEGIN_NAMESPACE //说明本菜单栏定义于 Qt的全局命名空间
class QMenuBarPrivate;
class QStyleOptionMenuItem;
class QWindowsStyle;
class QPlatformMenuBar;
/*
The QMenuBar class provides a horizontal menu bar.
不需要设置菜单栏。它会自动将其几何形状设置为父控件的顶部,并在父控件重新调整大小时适当地进行更改。
Usage:
在大多数主要窗口样式应用程序中,
您将使用QMainWindow中提供的menuBar()函数,将QMenus添加到菜单栏中,并将QActions添加到弹出菜单中。
可以通过使用QWidgetAction类的实例来将小部件添加到菜单中。
然后,可以使用通常的方式将这些动作插入菜单中;请参阅QMenu文档以获取详细信息。
Platform Dependent Look and Feel:
不同的平台对菜单栏的外观以及用户与菜单栏交互时的行为有不同的要求。
例如,Windows 系统通常配置为仅在按下 AIt 键时显示带下划线的字符助记符,
这些助记符指示菜单栏中项目的键盘快捷方式。
QMenuBar as a Global Menu Bar :
在macOs和某些Linux桌面环境中,如Ubuntu Unity,
QMenuBar是用于使用系统级菜单栏的包装器.
如果在同一个对话框中有多个菜单栏,
则最外面的菜单栏(通常位于具有widget标志Qt::Window的widget中)将用于系统级菜单栏。
...........
*/
class Q_WIDGETS_EXPORT QMenuBar : public QWidget
{
Q_OBJECT
//此属性包含弹出窗口的方向. 默认弹出窗口的方向。默认情况下,菜单会“向下”弹出屏幕。
//通过将属性设置为true,菜单将“向上”弹出。您可以将此调用用于位于它们所指向的文档下方的菜单。
//如果菜单无法在屏幕上显示,则自动使用其他方向。
Q_PROPERTY(bool defaultUp //决定下拉菜单的弹出方向,默认向下弹出。
READ isDefaultUp WRITE setDefaultUp)
Q_PROPERTY(bool nativeMenuBar //本属性在 windows系统上无用
READ isNativeMenuBar WRITE setNativeMenuBar)
//这个属性表示在支持它的平台上是否将使用菜单栏作为原生菜单栏。
//此属性指定是否应在支持的平台上将菜单栏用作原生菜单栏。目前支持的平台是macOS和Linux桌面,
//它们使用com,canonical.dbusmeny D-Bus接口(例如Ubuntu Unity)。
//如果此属性为真,则菜单栏在原生菜单栏中使用,不在其父窗口中;如果为假,则菜单栏保留在窗口中。
//在其他平台上,设置此属性没有影响,读取此属性将始终返回假。
//默认情况下,会遵循是否为该应用程序设置了 Qt:AA_DontUseNativeMenuBar属性。
//显式设置此属性会覆盖属性的存在(或不存在)。
private:
Q_DECLARE_PRIVATE(QMenuBar)
Q_DISABLE_COPY(QMenuBar)
Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
Q_PRIVATE_SLOT(d_func(), void _q_internalShortcutActivated(int))
Q_PRIVATE_SLOT(d_func(), void _q_updateLayout())
friend class QMenu ; //本菜单栏类的友元类是菜单 QMenu
friend class QMenuPrivate ;
friend class QWindowsStyle;
public:
explicit QMenuBar(QWidget * parent = nullptr);
~QMenuBar();
// Q_PROPERTY(bool defaultUp //决定下拉菜单的弹出方向,默认向下弹出。
// READ isDefaultUp WRITE setDefaultUp)
bool isDefaultUp() const;
void setDefaultUp(bool);
// Q_PROPERTY(bool nativeMenuBar //本属性在 windows系统上无用
// READ isNativeMenuBar WRITE setNativeMenuBar)
bool isNativeMenuBar() const;
void setNativeMenuBar(bool nativeMenuBar);
QPlatformMenuBar * platformMenuBar(); //无注释
//菜单栏里的按钮是由 QMenu::menuAction()得到的。
//总结:以为菜单栏里插入的是菜单,其实插入的是对应代表菜单的按钮QAction。
//Returns the QAction that is currently highlighted, if any, else nullptr.
QAction * activeAction() const; //显示菜单栏上被高亮显示的按钮
void setActiveAction(QAction * action);
//Sets the currently highlighted action to action.
//这俩函数的意思是不必为每个按钮单独设置触发与高亮函数,只为按钮的容器,菜单栏或菜单设计槽函数即可。
//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.
//enum Qt::Corner { TopLeftCorner = 0, TopRightCorner = 1,
// BottomLeftCorner = 2, BottomRightCorner = 3 };
QWidget * cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const;
void setCornerWidget(QWidget * w,
Qt::Corner corner = Qt::TopRightCorner);
//This sets the given w 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.
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
int heightForWidth(int) const override;
QRect actionGeometry(QAction *) const; //因为本菜单栏也可以直接管理按钮
QAction * actionAt(const QPoint &) const; //略,这俩函数在 QMenu里也出现过
void clear(); //Removes all the actions from the menu bar.
//This convenience function inserts menu before action before and returns the
// menus menuAction(). //把菜单 menu 放到 before的前面,并返回 menu对应的 QAction
QAction * insertMenu(QAction * before, QMenu * menu); //在菜单里也可以插入子菜单
QAction * addMenu(QMenu * menu); //在菜单栏的末尾追加菜单,返回菜单对应的按钮。
//Appends menu to the menu bar. Returns the menu's menuAction().
//The menu bar does not take ownership of the menu.
QMenu * addMenu(const QString & title);
//生成一个菜单,其对应的按钮项叫 title
//Appends a new QMenu with title to the menu bar.
//The menu bar takes ownership of the menu. Returns the new menu.
QMenu * 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.
//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.
QAction * insertSeparator(QAction * before); //在菜单栏里 before按钮的前面插入分隔符
QAction * addSeparator(); //Appends a separator to the menu.
//经测试,新添加的分隔符的容器父类,就是菜单栏。但经测试,菜单栏里似乎不允许或不显示添加的分隔符。
//该 addAction() 函数的正确用法如下:可见 QMenu::menuAction()这个函数很重要!!
// menubar->addAction(menu_F->menuAction()); //为菜单栏添加"文件(&F)"菜单
// menu_F ->setTitle(
// QCoreApplication::translate("MainWindow", //设置菜单的名字,以及快捷键
// "\346\226\207\344\273\266(&F)", nullptr));
using QWidget::addAction;
//void QWidget::addAction(QAction * action);
QAction * addAction(const QString & text); //说明菜单栏里可以添加普通按钮
//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.
QAction * addAction(const QString & text, //为菜单栏创建按钮,并为其指定槽函数。
const QObject * receiver, const char * member);
// addAction(QString):
//Connect to a QObject slot / functor or function pointer (with context)
template<typename Obj, typename Func1>
inline typename std::enable_if<
!std::is_same<const char *, Func1>::value
&& QtPrivate::IsPointerToTypeDerivedFromQObject<Obj *>::Value,
QAction *>::type
addAction(const QString & text, const Obj * object, Func1 slot)
{ //把创建的文本为 text的按钮的 triggered()信号连接到形参 object的 slot槽函数上
QAction * result = addAction(text);
connect(result, & QAction::triggered, object, std::move(slot));
return result;
}
// addAction(QString):
//Connect to a functor or function pointer (without context)
template <typename Func1>
inline QAction * addAction(const QString & text, Func1 slot)
{ //把创建的 text按钮的信号连接到全局槽函数 slot上。
QAction * result = addAction(text);
connect(result, & QAction::triggered, std::move(slot));
return result;
}
public Q_SLOTS:
void setVisible(bool visible) override; //菜单栏消失,窗体整体上移,工具栏占据了菜单栏的位置。
//Reimplements an access function for property: QWidget::visible.
Q_SIGNALS:
//This signal is emitted when an action in a menu belonging to this menubar
//is triggered as a result of a mouse click; //注意上面的话:an action in a menu
//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.
//即使菜单栏里的按钮被敲击,也不会触发 triggered()信号。
void triggered(QAction * action); //菜单栏里的菜单里的按钮被鼠标敲击,才触发本信号。
void 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.
protected:
virtual void initStyleOption(QStyleOptionMenuItem * option,
const QAction * action) const;
bool eventFilter(QObject *, QEvent *) override;
bool event(QEvent *) override;
void changeEvent(QEvent *) override;
void keyPressEvent(QKeyEvent *) override;
void mouseReleaseEvent(QMouseEvent *) override;
void mousePressEvent(QMouseEvent *) override;
void mouseMoveEvent(QMouseEvent *) override;
void leaveEvent(QEvent *) override;
void paintEvent(QPaintEvent *) override;
void resizeEvent(QResizeEvent *) override;
void actionEvent(QActionEvent *) override;
void focusOutEvent(QFocusEvent *) override;
void focusInEvent(QFocusEvent *) override;
void timerEvent(QTimerEvent *) override;
}; //完结 class QMenuBar : public QWidget
QT_END_NAMESPACE
#endif // QMENUBAR_H
(8)
谢谢