QT6 源(111):阅读与注释菜单栏 QMenuBar,进行属性与成员函数测试,信号与槽函数测试,并给出源码

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

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值