QT6 源(113)篇二:阅读与注释工具栏 QToolBar,给出源码

(9)本源码来自于头文件 qtoolbar . h

#ifndef QDYNAMICTOOLBAR_H
#define QDYNAMICTOOLBAR_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qaction.h>
#include <QtWidgets/qwidget.h>

QT_REQUIRE_CONFIG(toolbar);

QT_BEGIN_NAMESPACE

class QToolBarPrivate;

class QAction;
class QIcon;
class QMainWindow;
class QStyleOptionToolBar;

/*
The QToolBar class provides a movable panel that contains a set of controls.

工具栏按钮是通过添加操作、使用addAction()或insertAction()来添加的。
可以使用addSeparator()或insertSeparator()分隔按钮组。
如果工具栏按钮不合适,可以使用addWidget()或insertWidget()插入一个控件。
合适的控件示例包括QSpinBox、QDoubleSpinBox和QComboBox。
当按下工具栏按钮时,它会发出actionTriggered()信号。

工具栏可以固定在特定区域(例如窗口顶部),或者可以在工具栏区域之间移动;
请参setMovable()、isMovable()、alowedAreas()和isAreaAllowed()。

当工具栏的尺寸调整到太小以至于无法显示其中包含的所有项目时,
扩展按钮将作为工具栏中的最后一个项目出现。按下扩展按钮将弹出一个包含当前不适合工具栏的项目的菜单。

当一个QToolBar不是QMainWindow的子项时,
它就会失去使用addWidget()添加到工具栏的扩展弹出窗口填充小部件的能力。
请使用通过继承OWidgetAction并实现QWidgetAction::createWidget()创建的小部件动作。


    enum    ToolButtonStyle { //本属性描述工具栏的按钮风格
            ToolButtonIconOnly,
            ToolButtonTextOnly,
            ToolButtonTextBesideIcon,
            ToolButtonTextUnderIcon ,
            ToolButtonFollowStyle
    };

    enum      ToolBarArea {  //本属性用于描述工具栏可以摆放的位置
            NoToolBarArea  =   0,
          LeftToolBarArea  = 0x1,
         RightToolBarArea  = 0x2,
           TopToolBarArea  = 0x4,
        BottomToolBarArea  = 0x8,
         ToolBarArea_Mask  = 0xf,
           AllToolBarAreas = ToolBarArea_Mask
    };
    Q_DECLARE_FLAGS(ToolBarAreas, ToolBarArea)
    Q_DECLARE_OPERATORS_FOR_FLAGS(ToolBarAreas)
*/

class Q_WIDGETS_EXPORT QToolBar : public QWidget //工具栏可装入按钮、分隔符,还有控件
{
    Q_OBJECT

    //工具栏的方向。 默认值是 Qt::Horizontal。当工具栏由 QMainWindow管理时,不应使用此函数。
    //如果您想将已添加到主窗口的工具栏移动到另一个Qt:ToolBarArea,
    //可以使用QMainWindow::addToolBar()或QMainWindow::insertToolBar()。
    Q_PROPERTY(Qt::Orientation      orientation    //工具栏的方向.不应使用本属性。
                READ                orientation    WRITE   setOrientation
                NOTIFY              orientationChanged)

    Q_PROPERTY(QSize                iconSize       //工具栏里的按钮图片的最大大小
                READ                iconSize       WRITE   setIconSize
                NOTIFY              iconSizeChanged) //小图片不会因此而扩展
    //size of icons in the toolbar.
    //The default size is determined by the application's style and is derived from the
    //  QStyle::PM_ToolBarIconSize pixel metric. It is the maximum size an icon can have.
    //Icons of smaller size will not be scaled up.

    //This property holds whether the user can move the toolbar within the toolbar area,
    //or between toolbar areas.   By default, this property is true.
    //This property only makes sense if the toolbar is in a QMainWindow.
    Q_PROPERTY(bool                 movable        //表本工具栏是否可在允许的上下左右区域间移动
                READ              isMovable        WRITE   setMovable
                NOTIFY              movableChanged)

    Q_PROPERTY(Qt::ToolBarAreas     allowedAreas   //上下左右
                READ                allowedAreas   WRITE   setAllowedAreas
                NOTIFY              allowedAreasChanged)
    //areas where the toolbar may be placed。The default is Qt::AllToolBarAreas.
    //This property only makes sense if the toolbar is in a QMainWindow.
    //定义  ToolBarAreas = QFlas<ToolBarArea>

    Q_PROPERTY(bool                 floatable      //表本工具栏是否可被拖拽到屏幕上的任意区域
                READ              isFloatable      WRITE   setFloatable)
    //This property holds whether the toolbar can be dragged and dropped as an
    //  independent window.   The default is true.

    //This property holds whether the toolbar is an independent window.
    //By default, this property is true.
    Q_PROPERTY(bool                floating        READ     isFloating)

    //This property holds the style of toolbar buttons。
    //This property defines the style of all tool buttons that are added as QActions.
    //Note that if you add a QToolButton with the addWidget() method,
    //  it will not get this button style.
    //To have the style of toolbuttons follow the system settings,
    //  set this property to Qt::ToolButtonFollowStyle.
    //  On Unix, the user settings from the desktop environment will be used.
    //  On other platforms, Qt::ToolButtonFollowStyle means icon only.
    Q_PROPERTY(Qt::ToolButtonStyle       toolButtonStyle    //设置工具栏的按钮类 QAction的样式
                READ                     toolButtonStyle    WRITE    setToolButtonStyle
                NOTIFY                   toolButtonStyleChanged)


private:
    Q_DECLARE_PRIVATE(QToolBar)
    Q_DISABLE_COPY(QToolBar)
    Q_PRIVATE_SLOT(d_func(), void _q_toggleView(bool))
    Q_PRIVATE_SLOT(d_func(), void _q_updateIconSize(const QSize &))
    Q_PRIVATE_SLOT(d_func(), void _q_updateToolButtonStyle(Qt::ToolButtonStyle))

    friend class QMainWindow       ;
    friend class QMainWindowLayout ;
    friend class QToolBarLayout    ;
    friend class QToolBarAreaLayout;

public:
    //Constructs a QToolBar with the given parent.
    explicit QToolBar(QWidget * parent = nullptr); //toolBar = new QToolBar(MainWindow);

    explicit QToolBar(const QString & title, QWidget * parent = nullptr);
    //Constructs a QToolBar with the given parent. //title字段在上下文菜单里才有效。
    //The given window title identifies the toolbar and is shown in the
    //  context menu provided by QMainWindow.


    ~QToolBar();


//   Q_PROPERTY(Qt::Orientation      orientation    //工具栏的方向.不应使用本属性。
//               READ                orientation    WRITE   setOrientation
//               NOTIFY              orientationChanged)
                Qt::Orientation      orientation() const;
                void              setOrientation(Qt::Orientation orientation);
Q_SIGNALS:
                void                 orientationChanged(Qt::Orientation orientation);

public :
//   Q_PROPERTY(QSize                iconSize       //工具栏里的按钮图片的最大大小
//               READ                iconSize       WRITE   setIconSize
//               NOTIFY              iconSizeChanged) //小图片不会因此而扩展
                QSize                iconSize() const;
public Q_SLOTS:
                void              setIconSize       (const QSize & iconSize);
Q_SIGNALS:
                void                 iconSizeChanged(const QSize & iconSize);

public :
//   Q_PROPERTY(bool                 movable        //表本工具栏是否可在允许的上下左右区域间移动
//               READ              isMovable        WRITE   setMovable
//               NOTIFY              movableChanged)
                bool               isMovable() const;
                void              setMovable       (bool movable);
Q_SIGNALS:
                void                 movableChanged(bool movable);

public :
//   Q_PROPERTY(Qt::ToolBarAreas     allowedAreas   //上下左右
//               READ                allowedAreas   WRITE   setAllowedAreas
//               NOTIFY              allowedAreasChanged)
                Qt::ToolBarAreas     allowedAreas() const;
                void              setAllowedAreas       (Qt::ToolBarAreas         areas);
Q_SIGNALS:
                void                 allowedAreasChanged(Qt::ToolBarAreas  allowedAreas);

public :
//   Q_PROPERTY(bool                 floatable      //表本工具栏是否可被拖拽到屏幕上的任意区域
//               READ              isFloatable      WRITE   setFloatable)
                bool               isFloatable() const;
                void              setFloatable(bool floatable);


//   Q_PROPERTY(bool                 floating       READ     isFloating)
                bool               isFloating() const;


//   Q_PROPERTY(Qt::ToolButtonStyle       toolButtonStyle    //设置工具栏的按钮类 QAction的风格
//               READ                     toolButtonStyle    WRITE    setToolButtonStyle
//               NOTIFY                   toolButtonStyleChanged)
                Qt::ToolButtonStyle       toolButtonStyle() const;
public Q_SLOTS:
        void     setToolButtonStyle       (Qt::ToolButtonStyle   toolButtonStyle);
Q_SIGNALS:
        void        toolButtonStyleChanged(Qt::ToolButtonStyle   toolButtonStyle);

public  :
    //Returns true if this toolbar is dockable可停靠的 in the given area;
    //otherwise returns false.
    inline bool isAreaAllowed(Qt::ToolBarArea area) const
    { return (allowedAreas() & area) == area; }

    //Returns the widget associated with the specified action.
    QWidget * widgetForAction(QAction * action) const; //返回与形参按钮关联的控件。
    //Inserts the given widget in front of the toolbar item associated with the
    //  before action.                   在 before按钮的前面插入控件 widget。
    //Note: You should use QAction::setVisible() to change the visibility of the widget.
    //Using QWidget::setVisible(), QWidget::show() and QWidget::hide() does not work.
    //返回控件对应的 QAction对象的意义就在于用其控制 QWidget控件的可见性。
    QAction * insertWidget(QAction * before, QWidget * widget); //往工具栏里添加 QWidget控件
    QAction *    addWidget(QWidget * widget);
    //Adds the given widget to the toolbar as the toolbar's last item.
    //The toolbar takes ownership of widget.  //工具栏会容纳这个控件,完成对其将来的析构。
    //If you add a QToolButton with this method,
    //  the toolbar's Qt::ToolButtonStyle will not be respected不会被参考.

    QRect     actionGeometry(QAction * action) const;

    QAction * actionAt(const QPoint & p) const;
    inline
    QAction * actionAt(int x,   int   y) const { return actionAt(QPoint(x, y)); }

    //Returns a checkable action that can be used to show or hide this toolbar.
    //The action's text is set to the toolbar's window title.
    QAction * toggleViewAction() const; //新建的 toolBar按钮,以工具栏为父容器,故而不会内存泄露。
    //QToolBar继承于 QWidget,故也有 windowTitle属性,默认取值为"toolBar"。
    //把该 toolBar按钮添加到窗体后,点击它就会隐藏或开启工具栏了。

    void clear(); //Removes all actions from the toolbar.清空按钮与控件,剩余空的工具栏。

    using   QWidget::addAction; //void  QWidget::addAction(QAction * action);添加按钮
    QAction * addAction(const QString & text);    //这些与菜单栏是重复的

    QAction * addAction(const QString & text,     //添加按钮并指定按钮的槽函数
                        const QObject * receiver, const char    * member);

    QAction * addAction(const QIcon   & icon,     const QString & text);

    QAction * addAction(const QIcon   & icon,     const QString & text,
                        const QObject * receiver, const char    * member);

    // addAction(QString):
    //Connect to a QObject slot / functor or function pointer (with context)
    template<class 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)
    {
        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)
    {
        QAction * result = addAction(text);
        connect(result, & QAction::triggered, slot);
        return result;
    }

    // addAction(QString):
    //Connect to a QObject slot / functor or function pointer (with context)
    template<class Obj, typename Func1>
    inline typename std::enable_if<
        !  std::is_same<const char*, Func1>::value
        && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value,
                                        QAction *>::type
    addAction(const QIcon   & actionIcon,
              const QString & text, const Obj * object, Func1 slot)
    {
        QAction *result = addAction(actionIcon, text);
        connect(result, &QAction::triggered, object, std::move(slot));
        return result;
    }

    // addAction(QIcon, QString):
    //Connect to a functor or function pointer (without context)
    template <typename Func1>
    inline QAction * addAction(const QIcon & actionIcon, const QString & text, Func1 slot)
    {
        QAction *result = addAction(actionIcon, text);
        connect(result, &QAction::triggered, slot);
        return result;
    }

    QAction * insertSeparator(QAction * before); //往工具栏里添加分隔符
    QAction *    addSeparator();

public Q_SLOTS:
    //void setIconSize       (const         QSize &  iconSize       );
    //void setToolButtonStyle(Qt::ToolButtonStyle    toolButtonStyle);

Q_SIGNALS:
    //This signal is emitted when an action in this toolbar is triggered.
    //This happens when the action's tool button is pressed,
    //or when the action is triggered in some other way outside the toolbar.
    //The parameter holds the triggered action. //工具栏里的按钮被触发,发射此信号
    void          actionTriggered(QAction    *          action);

    //This signal is emitted when the floating property changes.
    //The topLevel parameter is true if the toolbar is now floating;
    //otherwise it is false. 当工具栏真的浮动起来或撤销浮动时,均会触发本信号函数。
    void          topLevelChanged(bool                  topLevel       );

    //This signal is emitted when the toolbar becomes visible (or invisible).
    //This happens when the widget is hidden or shown.
    void        visibilityChanged(bool                  visible        );

    //  This signal is emitted when the collection of allowed areas for the
    //      toolbar is changed. The new areas in which the toolbar can be positioned are
    //  specified by allowedAreas.
    //void    allowedAreasChanged(Qt::ToolBarAreas      allowedAreas   ); //函数原型
    //void     orientationChanged(Qt::Orientation       orientation    ); //函数原型
    //  This signal is emitted when the orientation of the toolbar changes.
    //  The orientation parameter holds the toolbar's new orientation.

    //  This signal is emitted when the tool button style is changed.
    //  The toolButtonStyle parameter holds the toolbar's new tool button style.
    //void toolButtonStyleChanged(Qt::ToolButtonStyle   toolButtonStyle); //函数原型
    //void         movableChanged(bool                  movable        ); //函数原型
    //  This signal is emitted when the toolbar becomes movable or fixed.
    //  If the toolbar can be moved, movable is true; otherwise it is false.

    //  This signal is emitted when the icon size is changed.
    //  The iconSize parameter holds the toolbar's new icon size.
    //void        iconSizeChanged(const QSize         & iconSize       ); //函数原型

protected:
    virtual void initStyleOption(QStyleOptionToolBar * option) const;

    bool        event(QEvent       * event) override;
    void  actionEvent(QActionEvent * event) override;
    void  changeEvent(QEvent       * event) override;
    void   paintEvent(QPaintEvent  * event) override;


}; //完结 class QToolBar : public QWidget


QT_END_NAMESPACE

#endif // QDYNAMICTOOLBAR_H

(10)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangzhangkeji

谢谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值