QT6 源(118):阅读与注释工具按钮 QToolButton,先给出本类的继承关系,接着测试属性部分,并给出源代码,综合测试举例见小知识篇 16

(1)先给出本类的继承关系

在这里插入图片描述

(2)接着开始学习本类的属性部分

在这里插入图片描述

++

在这里插入图片描述

++

在这里插入图片描述

++

在这里插入图片描述

(3) 接着继续学习成员函数 :略。本按钮的成员函数与 QPushButton 高度相似,唯一的不同就是本 ToolButton 支持与 QAction 的绑定。 补充:也有多个不同:QToolButton 支持与 QAction 的绑定;QToolButton 支持绑定一个弹出菜单;QToolButton 可以放到工具栏上。测试本类的成员函数,需要的支持类与素材太多,只能给出综合案例里的测试了 ,不便于单独进行测试。测试举例在这里

(4) 给出本类的信号函数的测试,取材也来自于上面的综合测试:

在这里插入图片描述

(5)给出本类的源代码 ,来自于头文件 qtoolbutton . h

#ifndef QTOOLBUTTON_H
#define QTOOLBUTTON_H

#include <QtWidgets/qtwidgetsglobal.h>

#include <QtWidgets/qabstractbutton.h>

QT_REQUIRE_CONFIG(toolbutton);

QT_BEGIN_NAMESPACE

class QToolButtonPrivate;
class QMenu;
class QStyleOptionToolButton;


/*
The QToolButton class provides a quick-access button to commands or options,
usually used inside a QToolBar.

工具按钮是一种特殊的按钮,它提供了快速访问特定命令或选项的功能。
与普通的命令按钮不同,工具按钮通常不显示文本标签,而是显示图标。

工具按钮通常在创建新的 QAction 实例或使用 QToolBar::addAction()将现有动作添加到工具栏时创建。
也可以像任何其他小部件一样以相同的方式构造工具按钮,并在布局中将它们与其他小部件排列在一起。

工具按钮的一个经典用法是选择工具;例如,绘图程序中的“笔”工具。
这可以通过使用QToolButton作为切换按钮(见setCheckable())来实现。

QToolButton 支持自动提升。在自动提升模式下,只有当鼠标指向按钮时,按钮才会绘制一个3D框架。
当在 QToolBar 中使用按钮时,此功能会自动打开。使用 setAutoRaise()函数可以更改它。
属性 auto-raising其实就是不再绘制按钮的边框而已。

工具按钮的图标设置为Qlcon。这使得可以为禁用状态和活动状态指定不同的pixmap。
当按钮的功能不可用时,将使用禁用的pixmap。
当鼠标指针悬停在按键上时,按键自动升起,活动的点阵图就会显示出来。

按钮的外观和尺寸可通过 setToolButtonStyle()和setlconSize()进行调整。
当在QMainWindow的OToolBar中使用时,该按钮会自动调整为QMainWindow的设置
(参见QMainWindow::setToolButtonStyle()和QMainWindow::setlconSize())。
工具按钮还可以显示箭头符号,而不是图标,由arrowType指定。

工具按钮可以在弹出菜单中提供额外的选择。可以使用setMenu()设置弹出菜单。
使用setPopupMode()来配置使用菜单集的工具按钮的不同模式。
默认模式是DelayedPopupMode,有时与Web浏览器的“后退”按钮一起使用。
按下并长按按钮一段时间后,会弹出一个菜单,显示可跳转的页面列表。
超时时间取决于样式,请参QStyle:SH ToolButton PopupDelay。


*/


class Q_WIDGETS_EXPORT QToolButton : public QAbstractButton
{
    Q_OBJECT

    Q_ENUMS(Qt::ToolButtonStyle    Qt::ArrowType) //往元对象系统里注册这两个枚举类

    //enum Qt::ArrowType { NoArrow, UpArrow, DownArrow, LeftArrow, RightArrow };
    //这个属性表示按钮是否显示箭头而不是普通图标。
    //这将箭头显示为QToolButton的图标。  默认情况下,此属性设置为Qt::NoArrow。
    Q_PROPERTY(Qt::ArrowType     arrowType
                READ             arrowType   WRITE   setArrowType)

    //此属性表示工具按钮是否仅显示图标、仅显示文本或在图标旁边/下方显示文本。
    //默认为Qt::ToolButtonlconOnly.
    //要使工具按钮的样式遵循系统设置,请将此属性设置为Qt::ToolButtonFollowStyle。
    //在Unix上,将使用来自桌面环境的用户设置。在其他平台上,Qt::ToolButtonFollowStyle表示仅图标。
    //QToolButton会自动将此槽连接到它所驻留的QMainWindow中的相关信号。
    Q_PROPERTY(Qt::ToolButtonStyle     toolButtonStyle
                READ                   toolButtonStyle   WRITE    setToolButtonStyle)

    //This property holds whether auto-raising is enabled or not.
    //The default is disabled (i.e. false).
    //This property is currently ignored on macOS when using QMacStyle.
    Q_PROPERTY(bool    autoRaise             //本属性默认为F,就是要绘制按钮边框
                READ   autoRaise             WRITE    setAutoRaise)


    Q_PROPERTY(ToolButtonPopupMode     popupMode  //延时弹出的意思是要多按压一会才弹出菜单
                READ                   popupMode  WRITE    setPopupMode)
    //describes the way that popup menus are used with tool buttons
    //By default, this property is set to DelayedPopup.

private:
    Q_DISABLE_COPY(QToolButton)
    Q_DECLARE_PRIVATE(QToolButton)

    Q_PRIVATE_SLOT(d_func(), void _q_buttonPressed())
    Q_PRIVATE_SLOT(d_func(), void _q_buttonReleased())
    Q_PRIVATE_SLOT(d_func(), void _q_updateButtonDown())
    Q_PRIVATE_SLOT(d_func(), void _q_menuTriggered(QAction*))

    Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())

public:  
    //Constructs an empty tool button with parent parent.
    explicit QToolButton(QWidget * parent = nullptr);

    ~QToolButton();


//   Q_PROPERTY(Qt::ArrowType      arrowType             //本属性设置按钮里箭头的样式
//              READ               arrowType    WRITE    setArrowType)
                Qt::ArrowType      arrowType() const;
                void            setArrowType(Qt::ArrowType type);


//   Q_PROPERTY(Qt::ToolButtonStyle     toolButtonStyle  //设置按钮里文字与图标的位置
//              READ                    toolButtonStyle
//              WRITE                setToolButtonStyle)
                Qt::ToolButtonStyle     toolButtonStyle() const;
public Q_SLOTS:
                void                 setToolButtonStyle(Qt::ToolButtonStyle style);

public :
//   Q_PROPERTY(bool      autoRaise         //本属性默认为F,就是要绘制按钮边框
//              READ      autoRaise         WRITE    setAutoRaise)
                bool      autoRaise() const;
                void   setAutoRaise(bool enable);


        //Describes how a menu should be popped up for tool buttons that has a menu set,
        //  or contains a list of actions.
        enum    ToolButtonPopupMode {
            DelayedPopup, //在按下并按住工具按钮一段时间(超时取决于样式,
            //请参见QStyle::SHToolButton_PopupDelay)后,菜单将显示。
            //一个典型的示例是某些网络浏览器工具栏中的“返回按钮。
            //如果用户单击它,浏览器将简单地返回到前一页;
            //如果用户按下并按住按钮一段时间,工具按钮将显示包含当前历史记录的菜单。
            //经测试,延迟菜单模式,也会在右下角显示个小三角符号,但点击没有效果,只有延时按压才弹出菜单。

            MenuButtonPopup, //在这种模式下,工具按钮会显示一个特殊箭头,以指示菜单的存在。
            //当按钮的箭头部分被按下时,菜单会显示出来。
            //经测试:点击按钮会触发 QAction动作,但只有点击下三角符号才会打开下拉菜单。

            InstantPopup  //当工具按钮被按下时,菜单会立即显示。
            //在这种模式下,不会触发按钮本身的动作。In this mode,
            //the button's own action is not triggered。按钮自己的 QAction动作不再被触发。
            //经测试:立即模式里也会显示下三角符号,但点击按钮的任意区域,不仅仅三角区,都会打开下拉菜单
        };
        Q_ENUM( ToolButtonPopupMode )
//   Q_PROPERTY(ToolButtonPopupMode     popupMode   //延时弹出的意思是要多按压一会才弹出菜单
//              READ                    popupMode   WRITE   setPopupMode)
                ToolButtonPopupMode     popupMode() const;
                void                 setPopupMode(ToolButtonPopupMode mode);


    //Returns the button's associated popup menu or nullptr
    //  if no popup menu has been set.
    QMenu *     menu() const; //本属性适合制作菜单栏里的按钮,都是带弹出菜单的。
    void     setMenu(QMenu * menu);
    //Associates the given menu with this tool button.
    //The menu will be shown according to the button's popupMode.
    //Ownership of the menu is not transferred to the tool button.

    QSize        sizeHint() const override;
    QSize minimumSizeHint() const override;

    //本 ToolButton相对于 PushButton的最大的区别就是此函数。
    QAction *   defaultAction() const; //Returns the default action.

public Q_SLOTS:
    void setDefaultAction(QAction * action);
    //Sets the default action to action. 本按钮的属性会接收 QAction的这些属性。
    //If a tool button has a default action,
    //the action defines the following properties of the button:
    //属性:checkable、checked、enabled、font、icon、statusTip、text、
    //  popupMode (assuming the action has a menu)、toolTip、whatsThis。
    //Other properties, such as autoRepeat, are not affected by actions.


    //经测试,菜单关联到按钮后,不必使用此函数,也可以控制菜单的打开关闭与使用,是使用鼠标。
    //显示(弹出)相关的弹出菜单。如果没有这样的菜单,此函数不会做任何事情。
    void showMenu(); //此函数在弹出菜单被用户关闭之前不会返回。

    //void setToolButtonStyle(Qt::ToolButtonStyle style); 这是一个槽成员函数

Q_SIGNALS:
    void triggered(QAction * action);
    //This signal is emitted when the given action is triggered.
    //The action may also be associated with other parts of the user interface,
    //such as menu items and keyboard shortcuts. Sharing actions in this way helps make the
    //user interface more consistent and is often less work to implement.

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

    //判断形参坐标在按钮的区域内
    bool hitButton(const QPoint & pos) const override; //继承自父类

    void      checkStateSet() override; //Reimplements: QAbstractButton::checkStateSet ().
    void  nextCheckState   () override; //Reimplements: QAbstractButton::nextCheckState().

    bool             event(QEvent       * e) override;
    void   mousePressEvent(QMouseEvent  *  ) override;
    void mouseReleaseEvent(QMouseEvent  *  ) override;
    void        paintEvent(QPaintEvent  *  ) override;
    void       actionEvent(QActionEvent *  ) override;
    void        enterEvent(QEnterEvent  *  ) override;
    void        leaveEvent(QEvent       *  ) override;
    void        timerEvent(QTimerEvent  *  ) override;
    void       changeEvent(QEvent       *  ) override;


}; //完结 class QToolButton : public QAbstractButton

QT_END_NAMESPACE

#endif // QTOOLBUTTON_H

(6)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值